头文件#include
// 仿函数.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
class Myconpare {
public:
bool operator()(int a, int b)
{
return a > b;
}
};
void test01()
{//template<calss T> T negate<T>//取反仿函数
negate<int>n;
cout << n(10) << endl;
//template<calss T> T plus<T>//加法仿函数
plus<int>p;
cout << p(1, 1) << endl;//1+1=2
}
void test02()
{
//templete<calss T> bool greater<T>
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//sort(v.begin(), v.end(), Myconpare());
sort(v.begin(), v.end(), greater<int>());//匿名对象
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
int main()
{
test02();
return 0;
}
函数适配器
// 仿函数.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
class Myconpare {
public:
bool operator()(int a, int b)
{
return a > b;
}
};
void test01()
{//template<calss T> T negate<T>//取反仿函数
negate<int>n;
cout << n(10) << endl;
//template<calss T> T plus<T>//加法仿函数
plus<int>p;
cout << p(1, 1) << endl;//1+1=2
}
void test02()
{
//templete<calss T> bool greater<T>
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//sort(v.begin(), v.end(), Myconpare());
sort(v.begin(), v.end(), greater<int>());//匿名对象
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
class Myrint :public binary_function<int,int, void>//参数类型,返回值类型
{
public:
void operator()(int val,int start)const
{
cout << "val " << val << " start " <<start << " " << val + start << " " << endl;
}
};
void test03()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
// for_each(v.begin(), v.end(), Myrint());
cout << "请输入起始值" << endl;
int num;
cin >> num;//让值加上num再输出
//绑定数据
for_each(v.begin(), v.end(), bind2nd( Myrint(),num));//将一个参数变为两个参数
for_each(v.begin(), v.end(), bind1st(Myrint(), num));//交换start和val的值
}
int main()
{
test03();
return 0;
}
函数适配器
0-9加起始值,进行输出,用户提供起始值
绑定
继承 binary_function<参数类型1,参数类型2,返回值类型>
// 仿函数.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<functional>
using namespace std;
class Myconpare {
public:
bool operator()(int a, int b)
{
return a > b;
}
};
void test01()
{//template<calss T> T negate<T>//取反仿函数
negate<int>n;
cout << n(10) << endl;
//template<calss T> T plus<T>//加法仿函数
plus<int>p;
cout << p(1, 1) << endl;//1+1=2
}
void test02()
{
//templete<calss T> bool greater<T>
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//sort(v.begin(), v.end(), Myconpare());
sort(v.begin(), v.end(), greater<int>());//匿名对象
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
class Myrint :public binary_function<int,int, void>//参数类型,返回值类型
{
public:
void operator()(int val,int start)const
{
cout << "val " << val << " start " <<start << " " << val + start << " " << endl;
}
};
void test03()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
// for_each(v.begin(), v.end(), Myrint());
cout << "请输入起始值" << endl;
int num;
cin >> num;//让值加上num再输出
//绑定数据
for_each(v.begin(), v.end(), bind2nd( Myrint(),num));//将一个参数变为两个参数
for_each(v.begin(), v.end(), bind1st(Myrint(), num));//交换start和val的值
}
//取反适配器
class Greater5: public unary_function<int ,bool>
{
public:
bool operator()(int val) const//确保不会改变继承得到的东西
{
return val > 5;
}
};
void test04()
{
//一元取反
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找大于5的数字
vector<int>::iterator it =find_if(v.begin(), v.end(), Greater5());
if (it != v.end())
{
cout << "find it" << *it << endl;
}
else {
cout << "can not find it" << endl;
}
//需求改为找到小于5的数 not1一元取反
cout << "---------------" << endl;
it = find_if(v.begin(), v.end(), not1(Greater5()));
//或 it = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(), 5)));
if (it != v.end())
{
cout << "find it" << *it << endl;
}
else {
cout << "can not find it" << endl;
}
/*
一元取反适配器
继承unary_function<参数类型1 ,返回值类型>
const修饰
*/
}
//函数指针适配器
class Myprint05{
public:
void operator()(int val)
{
cout << val << " " << endl;
}
};
void printmy05(int val,int start)
{
cout << val+start << " " << endl;
}
void test05()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//for_each(v.begin(), v.end(), Myprint05());
//或
//for_each(v.begin(), v.end(), printmy05);
//将函数指针 适配为 函数对象 ptr_fun
for_each(v.begin(), v.end(),bind2nd( ptr_fun(printmy05),100));
}
//成员函数适配器
class Person {
public:
Person(string name,int age)
{
this->name = name;
this->age = age;
}
void showperson()
{
cout << "成员函数中姓名" <<this->name << "年龄" <<this->age << endl;
}
void friend print06(Person& p);
void plus_age()
{
this->age = this->age + 100;
}
private:
string name;
int age;
};
void print06(Person& p)
{
cout << "姓名"<<p.name <<"年龄"<< p.age << endl;
}
void test06()
{
vector<Person> v;
Person p1("aa", 10);
Person p2("bb", 20);
Person p3("cc", 30);
Person p4("dd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
for_each(v.begin(), v.end(), print06);
cout << "--------------" << endl;
//成员函数适配器
//mem_fun_ref
for_each(v.begin(), v.end(), mem_fun_ref(&Person::plus_age));
for_each(v.begin(), v.end(),mem_fun_ref(&Person::showperson ));
}
int main()
{
test06();
return 0;
}