#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//replace(iterator begin, iterator end, oldValue, newValue);//将容器中所有oldValue替换为newValue
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test1401()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
v.push_back(4);
cout << "替换前:" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
replace(v.begin(), v.end(), 4, 10);
cout << "替换后:" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
}
int main()
{
test1401();
system("pause");
return 0;
}
网友评论