关于函数适配器less< Type>以及bind1st和bind2nd的使用
2016-05-23 22:45
525 查看
标准库定义了一组算术,关系与逻辑对象类,另外还定义了一组函数适配器
在functional头文件中:
plus< Type>
minus< Type>
negate< Type>
not_equal_to< Type>
logical_not< Type>….等等
包括算术、关系、逻辑对象类型
not1,not2
not1(bind2nd(less_equal< int>(),10)表示不小于等于10的那些元素
关于函数绑定器bind1st和bind2nd的使用,使用例子如下:
在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 */
相关文章推荐
- shell(一)
- win2008R2域环境配置用户主目录
- HTML之表单(笔记)
- python正则学习总结
- 安卓实战之如何快速搭建app架构
- 用类的友元函数,而不是成员函数定义复数类重载运算符+、-、*、/,使之能用于复数的加减乘除
- 选择排序—简单选择排序(Simple Selection Sort)
- PAT (Advanced Level) 1039. Course List for Student (25)
- Xcode添加静态库以及编译选项配置常见问题
- JavaScript高级程序设计(读书笔记)(一)
- 使用Object-C实现23种设计模式之单例模式
- ThreadLocal
- 数据库
- 利用ssh和scp在两台Ubuntu主机间传输文件
- Oracle的存储过程编程总结
- 个人随笔---为什么写博客
- linux查看当前系统有几个账户
- unity中类似NGUI或UGUI的onClick方法的可以调用任何公有方法的实现
- leetcode_c++:Jump Game II(045)
- Tomcat配置用户名和密码