目录
重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象,也叫仿函数(functor),其实就是重载“()”操作符,使得类对象可以像函数那样调用。
注意:
STL提供的算法往往都有两个版本,其中一个版本表现出最常用的某种运算,另一版本则允许用户通过template参数的形式来指定所要采取的策略。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class MyPrint
{
public:
void operator()(int num)
{
cout << "num " << num << endl;
count++;
}
int count = 0;
};
void MyPrint2(int num)
{
cout << "num " << num << endl;
}
void test01()
{
//MyPrint是一个类,而不是函数
MyPrint myPrint;
myPrint(111); //仿函数调用
//MyPrint2(111);
MyPrint()(1000);
}
//函数对象超出普通函数概念,内部可以保存状态
void test02()
{
MyPrint myPrint;
myPrint(111);
myPrint(111);
myPrint(111);
myPrint(111);
cout << "myPrint使用次数:" << myPrint.count << endl;
}
//函数对象作为参数
void doPrint(MyPrint print, int num)
{
print(num);
}
void test03()
{
doPrint(MyPrint(), 20);
}
int main()
{
//test01();
//test02();
test03();
system("pause");
return 0;
}
总结:
?????????谓词是指普通函数或重载的operator()返回值是bool类型的函数对象(仿函数)。如果operator接受一个参数,那么叫做一元谓词,如果接受两个参数,那么叫做二元谓词,谓词可作为一个判断式。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class GreaterThan20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
//一元谓词
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//查找第一个大于20的数字
//第三个参数 函数对象 匿名对象
/*GreaterThan20 greaterThan;
find_if(v.begin(), v.end(), greaterThan);*/
vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterThan20());
if (pos != v.end())
{
cout << "找到大于20的数字为:" << *pos << endl;
}
else
{
cout << "未找到" << endl;
}
}
//二元谓词
class MyCompare
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test02()
{
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(), MyCompare());
//匿名函数 lambda表达式 [](){};
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
STL内建了一些函数对象。分为:算数类函数对象,关系运算类函数对象,逻辑运算类仿函数。这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以产生无名的临时对象来履行函数功能。使用内建函数对象,需要引入头文件?#include<functional>。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//内建函数对象头文件
#include<functional>
void test01()
{
//template<class T> T negate<T>//取反仿函数
negate<int>n;
cout << n(10) << endl;
//template<class T> T plus<T>//加法仿函数
plus<int>p;
cout << p(1, 1) << endl;
}
//template<class T> bool greater<T>//大于
void test02()
{
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(), greater<int>()); //默认的是从小到大 less<int>()
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<vector>
#include<iostream>
#include<algorithm>
#include<functional>
#include<string>
using namespace std;
class MyPrint:public binary_function<int, int, void>
{
public:
void operator()(int v, int start) const
{
cout << "v=" << v << " start=" << start << " v+start=" << v + start << endl;
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
cout << "请输入起始值:" << endl;
int num;
cin >> num;
//for_each(v.begin(), v.end(), bind2nd( MyPrint(),num));
for_each(v.begin(), v.end(), bind1st(MyPrint(), num));
}
//第一步 绑定数据 bind2nd
//继承类 binary_function<参数类型1,参数类型2,返回值类型>
//加const修饰operator()
class GreaterThanFive: public unary_function<int,bool>
{
public:
bool operator()(int v) const
{
return v > 5;
}
};
//取反适配器
void test02()
{
//一元取反
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找大于5的数字
//需求改为小于5的数字
//vector<int>::iterator pos = find_if(v.begin(), v.end(), not1(GreaterThanFive()));
vector<int>::iterator pos = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(),5)));
if (pos != v.end())
{
cout << "找到小于5的数字:" << *pos << endl;
}
else
{
cout << "未找到" << endl;
}
}
//一元取反适配器 not1
//继承 unary_function <参数类型1,返回值类型>
//const修饰
void MyPrint03(int v, int start)
{
cout << "v+start=" << v + start << endl;
}
//函数指针适配器
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//将函数指针 适配为 函数对象
//ptr_fun
for_each(v.begin(), v.end(), bind2nd(ptr_fun(MyPrint03),100));
}
//成员函数适配器
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "成员函数中姓名:" << m_Name << " 年龄:" << m_Age << endl;
}
void PlusAge()
{
this->m_Age = this->m_Age + 100;
}
string m_Name;
int m_Age;
};
void MyPrintPerson(Person &p)
{
cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}
void test04()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 10);
Person p3("ccc", 10);
Person p4("ddd", 10);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//for_each(v.begin(), v.end(), MyPrintPerson);
//成员函数适配器
//mem_fun_ref
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::PlusAge));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
}
void test05(){
vector<Person*> v1;
//创建数据
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v1.push_back(&p1);
v1.push_back(&p2);
v1.push_back(&p3);
v1.push_back(&p4);
for_each(v1.begin(), v1.end(), mem_fun(&Person::showPerson));
}
int main()
{
//test01();
//test02();
//test03();
//test04();
test05();
system("pause");
return 0;
}