#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
class MyPrint {
public:
MyPrint() {
num = 0;
}
void operator()(int val) {
num++;
cout << val << endl;
}
public:
int num;
};
void test01() {
MyPrint myprint;
myprint(10);
}
int num = 0;
void MyPrint_S(int val) {
num++;
cout << val << endl;
}
void test02() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
MyPrint_S(10);
MyPrint_S(20);
cout << "MyPrint_S调用次数:" << num << endl;
MyPrint myprint;
MyPrint myprint_s = for_each(v.begin(), v.end(), myprint);
cout << "myprint.num:" << myprint.num << endl;
cout << "myprint_s.num:" << myprint_s.num << endl;
}
int main() {
test02();
cout << endl << endl;
return 0;
}
#include<vector>
#include<algorithm>
#include<functional>
#include<iostream>
using namespace std;
class MyPrint {
public:
void operator()(int v) {
cout << v << " ";
}
};
class MyPrintS : public binary_function<int, int, void> {
public:
void operator()(int v, int addnum) const {
cout << "v+addnum:" << v + addnum
<< " 第一个参数:" << v
<< " 第二个参数:" << addnum << endl;
}
};
void test01() {
vector<int> v;
for (int i = 0; i < 5; i++) {
v.push_back(i);
}
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
int addNum = 100;
cout << "bind1st:" << endl;
for_each(v.begin(), v.end(), bind1st(MyPrintS(), addNum));
cout << "bind2nd:" << endl;
for_each(v.begin(), v.end(), bind2nd(MyPrintS(), addNum));
}
struct MyGreater5 {
bool operator()(int v) {
return v > 5;
}
};
struct MyGreater5S : public unary_function<int, bool> {
bool operator()(int v) const {
return v > 5;
}
};
void test02() {
vector<int> v;
for (int i = 1; i < 11; i++) {
v.push_back(i);
}
cout << "元素:";
for_each(v.begin(), v.end(), MyPrint());
vector<int>::iterator it;
it = find_if(v.begin(), v.end(), MyGreater5());
cout << "\n第一个大于5的数:" << *it << endl;
it = find_if(v.begin(), v.end(), not1(MyGreater5S()));
cout << "第一个小于5的数:" << *it << endl;
}
struct MyCompare {
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
struct MyCompareS : public binary_function<int, int, bool> {
bool operator()(int v1, int v2) const {
return v1 > v2;
}
};
void test03() {
vector<int> v;
for (int i = 0; i < 5; i++) {
v.push_back(i);
}
cout << "排序前:" << endl;
for_each(v.begin(), v.end(), MyPrint());
sort(v.begin(), v.end(), MyCompare());
cout << "\n排序后:" << endl;
for_each(v.begin(), v.end(), MyPrint());
sort(v.begin(), v.end(), not2(MyCompareS()));
cout << "\n取反排序后:" << endl;
for_each(v.begin(), v.end(), MyPrint());
}
void MyPrint02(int v) {
cout << v << " ";
}
void test04() {
vector<int> v;
for (int i = 0; i < 5; i++) {
v.push_back(i);
}
cout << "直接调用普通函数:" << endl;
MyPrint02(5);
cout << "\n转化为函数对象后调用for_each:" << endl;
for_each(v.begin(), v.end(), ptr_fun(MyPrint02));
}
class Person {
public:
Person(int id, int age):id(id), age(age){
}
void show() {
cout << "id:" << id << " age:" << age << endl;
}
public:
int id;
int age;
};
void test05() {
vector<Person> v1;
Person p1(1, 20), p2(2, 21), p3(3, 18);
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
cout << "使用mem_fun_ref:" << endl;
for_each(v1.begin(), v1.end(), mem_fun_ref(&Person::show));
vector<Person*> v2;
v2.push_back(&p1);
v2.push_back(&p2);
v2.push_back(&p3);
cout << "\n使用mem_fun:" << endl;
for_each(v2.begin(), v2.end(), mem_fun(&Person::show));
}
int main() {
test05();
cout << endl << endl;
return 0;
}