预定义函数对象:STL模板库中封装的函数
函数适配器: 对于STL中的有些算法,其输入参数有些限制(比如参数个数等),因此需要用到函数适配器将参数适配成适合算法的输入,这个是我个人的理解。
1)预定义函数对象基本概念:标准模板库STL提前定义了很多预定义函数对象,#include <functional> 必须包含。
//1使用预定义函数对象:
//类模板plus<> 的实现了: 不同类型的数据进行加法运算
void main41()
{
plus<int> intAdd;
int x = 10;
int y = 20;
int z = intAdd(x, y); //等价于 x + y
cout << z << endl;
plus<string> stringAdd;
string myc = stringAdd("aaa", "bbb");
cout << myc << endl;
vector<string> v1;
v1.push_back("bbb");
v1.push_back("aaa");
v1.push_back("ccc");
v1.push_back("zzzz");
//缺省情况下,sort()用底层元素类型的小于操作符以升序排列容器的元素。
//为了降序,可以传递预定义的类模板greater,它调用底层元素类型的大于操作符:
cout << "sort()函数排序" << endl;;
sort(v1.begin(), v1.end(), greater<string>() ); //从大到小
for (vector<string>::iterator it=v1.begin(); it!=v1.end(); it++ )
{
cout << *it << endl;
}
}
2)算术函数对象
预定义的函数对象支持加、减、乘、除、求余和取反。调用的操作符是与type相关联的实例
加法:plus<Types>
plus<string> stringAdd;
sres = stringAdd(sva1,sva2);
减法:minus<Types>
乘法:multiplies<Types>
除法divides<Tpye>
求余:modulus<Tpye>
取反:negate<Type>
negate<int> intNegate;
ires = intNegate(ires);
Ires= UnaryFunc(negate<int>(),Ival1);
3)关系函数对象
等于equal_to<Tpye>
equal_to<string> stringEqual;
sres = stringEqual(sval1,sval2);
不等于not_equal_to<Type>
大于 greater<Type>
大于等于greater_equal<Type>
小于 less<Type>
小于等于less_equal<Type>
void main42()
{
vector<string> v1;
v1.push_back("bbb");
v1.push_back("aaa");
v1.push_back("ccc");
v1.push_back("zzzz");
v1.push_back("ccc");
string s1 = "ccc";
//int num = count_if(v1.begin(),v1.end(), equal_to<string>(),s1);
int num = count_if(v1.begin(),v1.end(),bind2nd(equal_to<string>(), s1));
cout << num << endl;
}
4)逻辑函数对象
逻辑与 logical_and<Type>
logical_and<int> indAnd;
ires = intAnd(ival1,ival2);
dres=BinaryFunc( logical_and<double>(),dval1,dval2);
逻辑或logical_or<Type>
逻辑非logical_not<Type>
logical_not<int> IntNot;
Ires = IntNot(ival1);
Dres=UnaryFunc( logical_not<double>,dval1);
10.3.2.7函数适配器
1)函数适配器的理论知识
2)常用函数函数适配器
标准库提供一组函数适配器,用来特殊化或者扩展一元和二元函数对象。常用适配器是:
1绑定器(binder): binder通过把二元函数对象的一个实参绑定到一个特殊的值上,将其转换成一元函数对象。C++标准库提供两种预定义的binder适配器:bind1st和bind2nd,前者把值绑定到二元函数对象的第一个实参上,后者绑定在第二个实参上。
2取反器(negator) : negator是一个将函数对象的值翻转的函数适配器。标准库提供两个预定义的ngeator适配器:not1翻转一元预定义函数对象的真值,而not2翻转二元谓词函数的真值。
常用函数适配器列表如下:
bind1st(op, value)
bind2nd(op, value)
not1(op)
not2(op)
mem_fun_ref(op)
mem_fun(op)
ptr_fun(op)
具体看如下代码:
#include <iostream>
#include "string"
#include <vector>
#include <list>
#include "set"
#include <algorithm>
#include "functional"
using namespace std;
//plus<int> 预定义的函数对象能实现不同类型的数据+运算
//实现了 数据类型 和 算法的分离 ==》通过函数对象技术来实现的...
//思考: 怎么样知道plus<type>是两个参数
void main()
{
plus<int> intAdd;
int x = 10;
int y = 20;
int z = intAdd(x,y);
cout << "z: " << z << endl; //输出 30
plus<string> stringAdd;
string s1 = "aaa";
string s2 = "bbb";
string s3 = stringAdd(s1,s2);
cout << "s3: " << s3 << endl; //输出: aaabbb
vector<string> v1;
v1.push_back("bbb");
v1.push_back("aaa");
v1.push_back("ccc");
v1.push_back("zzz");
v1.push_back("ccc");
v1.push_back("ccc");
sort(v1.begin(),v1.end(),greater<string>()); //从大到小排序
for (vector<string>::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << *it << endl;
}
/**输出:
zzz
ccc
ccc
ccc
bbb
aaa
*/
//求ccc出现的次数
string sc = "ccc";
//equal_to<string>() 有两个参数,left参数来自容器,right参数来自sc
//bind2nd函数适配器:把预定义函数对象 和 第二个参数进行绑定,函数适配器的作用是,在
//某些函数中只能接收固定数量的参数,函数适配器可以将不满足函数参数要求的输入适配成合适的函数输入
//故称为函数适配器
int num = count_if(v1.begin(),v1.end(),bind2nd(equal_to<string>(),sc));
cout << num << endl; //输出 3
}