unary_function和binay_function
2017-09-09 18:52
393 查看
一、unary_function详解
unary_function可以作为一个一元函数对象的基类,它只定义了参数和返回值的类型,本身并不重载()操作符,这个任务应该交由派生类去完成。其定义如下:template <class Arg, Class Result> struct unary_function { typedef Arg argument_type; typedef Result result_type; }
成员类型 | 定义 | 注释 |
---|---|---|
argument_type | 第一个模板参数 (Arg) | ()重载函数的参数类型 |
result_type | 第二个模板参数(Result) | ()重载函数的返回值类型 |
#include <iostream> // std::cout, std::cin #include <functional> // std::unary_function struct IsOdd : public std::unary_function<int,bool> { bool operator() (int number) {return (number % 2 != 0);} }; int main () { IsOdd IsOdd_object; IsOdd::argument_type input; IsOdd::result_type result; std::cout << "Please enter a number: "; std::cin >> input; result = IsOdd_object (input); std::cout << "Number " << input << " is " << (result ? "odd" : "even") << ".\n"; return 0; }
二、binary_function详解
binary_funciton 用来呈现二元函数的第一个参数型别、第二个参数类型,以及返回值类型 ,其定义如下:template <classArg1, classArg2, class Result> { typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type; }
成员类型 | 定义 | 注释 |
---|---|---|
first_argument_type | 第一个模板参数(Arg1) | 第一个模板参数(Arg1) |
second_argument_type | 第一个模板参数(Arg1) | ()重载函数的第二个参数类型 |
return_type | 第一个模板参数(Arg1) | ()重载函数的返回值类型 |
template <class T> struct plus : public binary_function <T, T, T> { T operator () (const T &x, const T &y) const {return x + y;} } Plus pluss; Plus::first_argument_type x; Plus::second_argument_type y; Plus::result_type reasult; x = 1; y = 2; reasult = pluss (x, y); std::cout << reasult << std::endl;
相关文章推荐
- 【codewar-7kyu】Unary function chainer
- [C/C++标准库]_[初级]_[unary_function 和 binary_function]
- 【C++ API】binary_function与unary_function使用详解
- [C/C++标准库]_[初级]_[unary_function 和 binary_function]
- std::unary_function 和 std::binary_function.
- 为什么在定义能够使用函数适配器的函数的时候要继承一下std::unary_function或者std::binary_function
- unary_function跟binary_function详解
- unary_function和binary_function详解
- error C2593: 'operator <<' is ambiguous error C2504: 'unary_function' : base class undefined
- std::unary_function
- 标准模板库介绍(一元及二元函数对象Unary and Binary Function Objects)
- [C/C++标准库]_[初级]_[unary_function 和 binary_function]
- Zend Server CE: undefined function mb_eregi_replace()
- [: ==: unary operator expected 解决方法
- Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ
- C++函数指针、函数对象与C++11 function对象对比分析
- warning:date()[function.date]:it is not safe to rely on the system' timezone setting
- $(function(){...});
- jquery 的(function(){});表示什么?
- Vue中SASS如何全局使用变量,mixin,或者function