#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
class myprint
{
public:
void operator()(int val) {
cout << val << endl;
}
};
void test() {
vector<int> v;
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
for_each(v.begin(), v.end(), myprint());
replace(v.begin(), v.end(),20,3000);
for_each(v.begin(), v.end(), myprint());
}
int main() {
test();
}
3000,30,40,50
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
class myprint
{
public:
void operator()(int val) {
cout << val << endl;
}
};
//谓词, 返回bool 类型
class great50
{
public:
bool operator()(int val)
{
return val > 30;
}
};
void test() {
vector<int> v;
v.push_back(60);
v.push_back(60);
v.push_back(60);
v.push_back(60);
for_each(v.begin(), v.end(), myprint());
replace_if(v.begin(), v.end(), great50(),3000);
for_each(v.begin(), v.end(), myprint());
}
int main() {
test();
}
60 60 60 60
3000 3000 3000 3000