目录
一、函数对象
重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象,也叫仿函数(functor),其实就是重载“()”操作符,使得类对象可以像函数那样调用。
注意:
1.函数对象(仿函数)是一个类,不是一个函数。
2.函数对象(仿函数)重载了”() ”操作符使得它可以像函数一样调用。
分类:假定某个类有一个重载的operator(),而且重载的operator()要求获取一个参数,我们就将这个类称为“一元仿函数”(unary functor);相反,如果重载的operator()要求获取两个参数,就将这个类称为“二元仿函数”(binary functor)。
函数对象的作用主要是什么?STL提供的算法往往都有两个版本,其中一个版本表现出最常用的某种运算,另一版本则允许用户通过template参数的形式来指定所要采取的策略。
//函数对象是重载了函数调用符号的类
class MyPrint
{
public:
MyPrint()
{
m_Num = 0;
}
int m_Num;
public:
void operator() (int num)
{
cout << num << endl;
m_Num++;
}
};
//函数对象
//重载了()操作符的类实例化的对象,可以像普通函数那样调用,可以有参数 ,可以有返回值
void test01()
{
MyPrint myPrint;
myPrint(20);
}
// 函数对象超出了普通函数的概念,可以保存函数的调用状态
void test02()
{
MyPrint myPrint;
myPrint(20);
myPrint(20);
myPrint(20);
cout << myPrint.m_Num << endl;
}
void doBusiness(MyPrint print,int num)
{
print(num);
}
//函数对象作为参数
void test03()
{
//参数1:匿名函数对象
doBusiness(MyPrint(),30);
}
总结:
1、函数对象通常不定义构造函数和析构函数,所以在构造和析构时不会发生任何问题,避免了函数调用的运行时问题。
2、函数对象超出普通函数的概念,函数对象可以有自己的状态
3、函数对象可内联编译,性能好。用函数指针几乎不可能
4、模版函数对象使函数对象具有通用性,这也是它的优势之一
二、谓词
谓词是指普通函数或重载的operator()返回值是bool类型的函数对象(仿函数)。如果operator接受一个参数,那么叫做一元谓词,如果接受两个参数,那么叫做二元谓词,谓词可作为一个判断式。
class Greater20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
class myCompare
{
public:
bool operator()(int v1 , int v2)
{
return v1 > v2;
}
};
//一元谓词
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的元素
vector<int>::iterator ret = find_if(v.begin(), v.end(), Greater20());
if (ret != v.end())
{
cout << "找到了元素大于20的值为: " << *ret << endl;
}
else
{
cout << "未找到" << endl;
}
//二元谓词
sort(v.begin(), v.end(), myCompare());
}
三、内建函数对象
STL内建了一些函数对象。
分为: 算数类函数对象, 关系运算类函数对象,逻辑运算类仿函数。
这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以产生无名的临时对象来履行函数功能。使用内建函数对象,需要引入头文件 #include<functional>。
- 6个算数类函数对象,除了negate是一元运算,其他都是二元运算。
template<class T> T plus<T>//加法仿函数 template<class T> T minus<T>//减法仿函数 template<class T> T multiplies<T>//乘法仿函数 template<class T> T divides<T>//除法仿函数 template<class T> T modulus<T>//取模仿函数 template<class T> T negate<T>//取反仿函数 |
- 6个关系运算类函数对象,每一种都是二元运算。
template<class T> bool equal_to<T>//等于 template<class T> bool not_equal_to<T>//不等于 template<class T> bool greater<T>//大于 template<class T> bool greater_equal<T>//大于等于 template<class T> bool less<T>//小于 template<class T> bool less_equal<T>//小于等于 |
- 逻辑运算类运算函数,not为一元运算,其余为二元运算。
template<class T> bool logical_and<T>//逻辑与 template<class T> bool logical_or<T>//逻辑或 template<class T> bool logical_not<T>//逻辑非 |
四、函数适配器
都要包含头文件:
#include <functional>
4.1 函数对象适配器
步骤:
- 1 绑定 bind2nd
- 2 继承 public binary_function<参数类型,参数类型 ,返回值类型>
- 3 加const
class MyPrint : public binary_function<int,int ,void>
{
public:
void operator()(int num , int start) const
{
cout << "num = " << num << " start = " << start << " sum = " << num + start << endl;
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
cout << "请输入起始累加值:" << endl;
int start;
cin >> start;
for_each(v.begin(), v.end(), bind2nd( MyPrint(), start) ); //推荐
//for_each(v.begin(), v.end(), bind1st(MyPrint(), start));
}
4.2 取反适配器
步骤:
- 1 取反适配器 not1
- 2 继承 public unary_function<参数类型,返回值类型>
- 3 加const
void test02()
{
//一元取反适配器
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
//vector<int>::iterator ret = find_if(v.begin(), v.end(), not1( GreaterFive()));
vector<int>::iterator ret = find_if(v.begin(), v.end(), not1( bind2nd( greater<int>(), 5 )));
if (ret != v.end())
{
cout << "找到小于5的数据为" << *ret << endl;
}
else
{
cout << "未找到" << endl;
}
//二元取反适配器
sort(v.begin(), v.end(), not2( less<int>()));
for (vector<int>::iterator it = v.begin(); it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
4.3 函数指针适配器
将myPrint函数指针 适配成函数对象即可。利用 ptr_fun
void myPrint(int val , int start)
{
cout << val + start << endl;
}
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
cout << "请输入起始累加值:" << endl;
int start;
cin >> start;
for_each(v.begin(), v.end(), bind2nd( ptr_fun(myPrint), start));
}
4.4 成员函数适配器
利用mem_fun_ref 将成员函数进行适配
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "姓名: " << this->m_Name << " 年龄: " << this->m_Age << endl;
}
void addAge()
{
this->m_Age++;
}
string m_Name;
int m_Age;
};
void test04()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
for_each(v.begin(), v.end(), mem_fun_ref( &Person::showPerson));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::addAge));
cout << "-------------------------" << endl;
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
}