C++中的标准函数对象
来源:互联网 发布:java做贪吃蛇游戏 编辑:程序博客网 时间:2024/04/30 12:26
转载地址:http://blog.csdn.net/chollima/article/details/3525934
函数对象函数对象是一种利用句法规则特定设计的类生成的对象,它的作用类似于函数。在C++中,这是通过在类中定义成员函数operator()实现的,举个例子:struct myclass { int operator()(int a) {return a;}}myobject;int x = myobject(0);它们作为断定或者比较功能和标准算法(algorithms)一起使用时将非常有用。在头文件<functional>中标准库提供了好几种标准的函数对象和一些修改方法以适应它们自己的行为。基类unary_function 一元的函数对象基类template <class Arg, class Result> struct unary_function { typedef Arg argument_type; typedef Result result_type; };binary_function 二元的函数对象基类template <class Arg1, class Arg2, class Result> struct binary_function { typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type; };算子类算术操作符plus + 加函数对象类 template <class T> struct plus : binary_function <T,T,T> { T operator() (const T& x, const T& y) const {return x+y;}};minus - 减函数对象类同理,这里就不一一列举multiplies * 乘函数对象类divides / 除函数对象类modulus % 模函数对象类negate - 负函数对象类比较操作符equal_to == 函数对象类用于相等比较template <class T> struct equal_to : binary_function <T,T,bool> { T operator() (const T& x, const T& y) const {return x==y;}};not_equal_to != 函数对象类用于不相等比较同理,这里就不一一列举greater > 函数对象类用于大于比较less < 函数对象类用于小于比较greater_equal >= 函数对象类用于不小于比较less_equal <= 函数对象类用于不大于比较逻辑操作符logical_and && 逻辑与函数对象类logical_or || 逻辑或函数对象类logical_not ! 逻辑非函数对象类适配器和转换功能否定器not1 返回一元函数对象的反面template <class Predicate>unary_negate<Predicate> not1 (const Predicate& pred){ return unary_negate<Predicate>(pred);}not2 返回二元函数对象的方面template <class Predicate>binary_negate<Predicate> not2 (const Predicate& pred){ return binary_negate<Predicate>(pred);}参数绑定器bind1st 返回绑定了第一个参数的函数对象template <class Operation, class T>binder1st<Operation> bind1st (const Operation& op, const T& x){ return binder1st<Operation>(op, typename Operation::first_argument_type(x));}bind2nd 返回绑定了第二个参数的函数对象template <class Operation, class T>binder2nd<Operation> bind2nd (const Operation& op, const T& x){ return binder2nd<Operation>(op, typename Operation::second_argument_type(x));}有用的类型unary_negate 生成一元函数对象类的反面template <class Predicate> class unary_negate : public unary_function <typename Predicate::argument_type,bool>{protected: Predicate fn;public: explicit unary_negate ( const Predicate& pred ) : fn (pred) {} bool operator() (const typename Predicate::argument_type& x) const { return !fn(x); }};binary_negate 生成二元函数对象类的反面template <class Predicate> class binary_negate : public binary_function <typename Predicate::first_argument_type, typename Predicate::second_argument_type, bool>{protected: Predicate fn;public: explicit binary_negate ( const Predicate& pred ) : fn (pred) {} bool operator() (const typename Predicate::first_argument_type& x, const typename Predicate::second_argument_type& y) const { return !fn(x,y); }};binder1st 绑定了第一个参数的函数对象类template <class Operation> class binder1st : public unary_function <typename Operation::second_argument_type, typename Operation::result_type>{protected: Operation op; typename Operation::first_argument_type value;public: binder1st ( const Operation& x, const typename Operation::first_argument_type& y) : op (x), value(y) {} typename Operation::result_type operator() (const typename Operation::second_argument_type& x) const { return op(value,x); }};binder2nd 绑定了第二个参数的函数对象类template <class Operation> class binder2nd : public unary_function <typename Operation::first_argument_type, typename Operation::result_type>{protected: Operation op; typename Operation::second_argument_type value;public: binder2nd ( const Operation& x, const typename Operation::first_argument_type& y) : op (x), value(y) {} typename Operation::result_type operator() (const typename Operation::first_argument_type& x) const { return op(x,value); }};还有一小部分没有翻译(这部分我还没看呢),以后另起补上。
0 0
- C++中的标准函数对象
- C++中的标准函数对象
- 标准C语言中的字符串处理函数
- 标准C语言中的字符串处理函数
- C标准库中的时间函数
- 标准c中的字符串处理函数
- 详解C标准库中的atexit()函数
- JavaScript中的标准对象
- C语言_标准库中的流函数
- golang 操作c 标准库中的CString函数注意事项
- 标准C语言中的文件操作函数的内存版
- Swift中的标准函数
- Swift2中的标准函数
- C语言--标准函数
- C标准函数篇
- 标准c数学函数
- 标准c内存函数
- 标准c数学函数
- Heroku Postgres, db migrate fail: alembic.util.exc.CommandError: Can't locate revision identified by
- java jxl读取excel中的数据传入二维数组中
- Java中的字符串常量池详解--转载自技术小黑屋
- Android资源--颜色
- Web前端性能优化之浏览器访问优化
- C++中的标准函数对象
- C++设计模式(一)装饰者
- 1-1 欢迎来的C++世界
- Web前端性能优化之CDN加速
- jquery validate (jquery 验证的三种方式)
- Linux学习---搜索命令
- 算法系列—快速排序及其优化(递归)
- js gbk与UTF8 转换
- Web前端性能优化之反向代理