14.34

来源:互联网 发布:大连民族大学网络教学 编辑:程序博客网 时间:2024/05/22 10:38
//编写类似于gt_cls的类,//但测试两个值是否相等。使用该对象和标准库算法编写程序,替换序列中给定的所有实例
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<tchar.h>
using namespace std;
class gt_cls
{
public:
 gt_cls(int ival=0):val(ival){}
 bool operator()(const int & iv)
 {
  return(iv == val);
 }
private:
 int val;
};
int _tmain(int argc, _TCHAR* argv[])
{
 std::cout << "intput some nums(ctrl + z to end):" << std::endl;
 vector<int>ivec;
 int ival;
 while (std::cin >> ival)
 {
  ivec.push_back(ival);
 }//end of input the ivec
 cin.clear();
 cout << "input a num which will be replaced:\t";
 int rp;
 cin >> rp;
 cout << "input a given num to insert:\t";
 int givennum;
 cin >> givennum;
 //replace
 replace_if(ivec.begin(), ivec.end(), gt_cls(rp), givennum);
 cout << "now,the new vector<int>ivec,is:\n\t";
 for (vector<int>::iterator it = ivec.begin(); it != ivec.end(); ++it)
 {
  cout << *it << "";
 }
 system("pause");
 return 0;
}