您的位置:首页 > 其它

关于函数适配器less< Type>以及bind1st和bind2nd的使用

2016-05-23 22:45 525 查看
标准库定义了一组算术,关系与逻辑对象类,另外还定义了一组函数适配器

在functional头文件中:

plus< Type>

minus< Type>

negate< Type>

not_equal_to< Type>

logical_not< Type>….等等

包括算术、关系、逻辑对象类型

sort(svec.begin(),svec.end(),greater< string>())

使用函数对象来对字符串降序排列

标准库还定义了两个求反器

not1,not2

not1(bind2nd(less_equal< int>(),10)表示不小于等于10的那些元素

关于函数绑定器bind1st和bind2nd的使用,使用例子如下:

#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () {
int numbers[] = {10,20,30,40,50,10};
int cx1,cx2,cx3;
cx1 = count_if (numbers, numbers+6, bind1st(equal_to<int>(),10) );//20=x
cx2 = count_if (numbers, numbers+6, bind1st(less<int>(),20) ); //20<x
cx3 = count_if (numbers, numbers+6, bind1st(greater<int>(),20) );//20>x
cout<<cx1<<endl<<cx2<<endl<<cx3<<endl;

cx1 = count_if (numbers, numbers+6, bind2nd(equal_to<int>(),10) );//20=x
cx2 = count_if (numbers, numbers+6, bind2nd(less<int>(),20) ); //x<20
cx3 = count_if (numbers, numbers+6, bind2nd(greater<int>(),20) );//x>20
cout<<cx1<<endl<<cx2<<endl<<cx3<<endl;
return 0;
}

/*
2
3
2

2
2
3
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: