[C/C++标准库]_[初级]_[unary_function 和 binary_function]
一元和二元函数.
场景:
1.C++算法库里经常出现unary_function和binary_function类型,比如sort,count_if,find_if,或者std::map的构造函数等,这是用来对元素进行比较用的类型.
template <class Arg, class Result> struct unary_function;
template <class Arg1, class Arg2, class Result> struct binary_function;
以下是从cplusplus.com摘录的说明:
二元函数转换为一元函数 bind2nd http://www.cplusplus.com/reference/functional/bind2nd/?kw=bind2nd template <class Operation, class T> binder2nd<Operation> bind2nd (const Operation& op, const T& x); Return function object with second parameter bound This function constructs an unary function object from the binary function object op by binding its second parameter to the fixed value x. To bind the first parameter to a specific value, see bind1st. 一元函数 unary_function http://www.cplusplus.com/reference/functional/unary_function/ 1.有返回值. 2.只有一个参数. In the case of unary function objects, this operator() member function takes a single parameter. unary_function is just a base class, from which specific unary function objects are derived. It has no operator() member defined (which derived classes are expected to define) - it simply has two public data members that are typedefs of the template parameters. It is defined as: template <class Arg, class Result> struct unary_function { typedef Arg argument_type; typedef Result result_type; }; 二元函数 binary_function http://www.cplusplus.com/reference/functional/binary_function/?kw=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; };
测试代码:
#include <stdio.h> #include <stdint.h> #include <time.h> #include <algorithm> #include <iostream> #include <functional> #include <vector> using namespace std; static void PrintVector(vector<int>& v) { cout << "....PrintVector...." << endl; for (int i = 0; i < v.size(); ++i) { cout << v[i] << endl; } } int main(int argc, char const *argv[]) { vector<int> data; srand( time(NULL) ); cout << "RAND_MAX: " << RAND_MAX << endl; for (int i = 0; i < 10; ++i) { data.push_back(rand()); } //1.std::less<int> sort(data.begin(),data.end(),std::less<int>()); PrintVector(data); //2.std::greator<int> sort(data.begin(),data.end(),std::greater<int>()); PrintVector(data); //3.std::greater_equal<int> //取>=0的整数 int numbers[]={20,-30,10,10,-40,0}; //bind2nd 把 binary_function 转换为 unary fucntion. int cx = std::count_if (numbers, numbers+6, std::bind2nd(std::greater_equal<int>(),0)); std::cout << "There are " << cx << " non-negative elements. "; vector<int> result; //1.找出所有大于0的整数,貌似没有好用的算法函数. for (int i = 0; i < 6; ++i) { if(numbers[i] > 0) { cout << numbers[i] << endl; } } //4.小于等于10的整数. cx = std::count_if (numbers, numbers+6, std::bind2nd(std::less_equal<int>(),10)); std::cout << "There are " << cx << " elements less or equal 10. "; return 0; }
输出:
RAND_MAX: 32767 ....PrintVector.... 1315 2075 8984 13167 14730 15153 18586 20698 23082 24151 ....PrintVector.... 24151 23082 20698 18586 15153 14730 13167 8984 2075 1315 There are 4 non-negative elements. 20 10 10 There are 5 elements less or equal 10.
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。