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.\n";
- 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.\n";
- 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
-
count_if :返回区间中满足指定条件的元素数目。
template<class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type count_if(
InputIterator _First,
InputIterator _Last,
Predicate _Pred
);
Parameters
_First 输入迭代器,指向将被搜索的区间第一个元素的位置。
_Last 输入迭代器,指向将被搜索的区间最后一个元素后面的。
_Pred 用户自定义的 predicate function object ,定义了元素被计数需满足的条件。 predicate 只带一个参数,返回 true 或 false.
Return Value
满足断言(predicate)指定条件的元素数。
Remarks
这个模板函数是书法count的泛化版本,用断言指定的条件代替等于一个指定的值。
Example
[cpp] view plaincopy- // alg_count_if.cpp
- // compile with: /EHsc
- #include <vector>
- #include <algorithm>
- #include <iostream>
- bool greater10(int value)
- {
- return value >10;
- }
- int main()
- {
- using namespace std;
- vector<int> v1;
- vector<int>::iterator Iter;
- v1.push_back(10);
- v1.push_back(20);
- v1.push_back(10);
- v1.push_back(40);
- v1.push_back(10);
- cout << "v1 = ( ";
- for (Iter = v1.begin(); Iter != v1.end(); Iter++)
- cout << *Iter << " ";
- cout << ")" << endl;
- vector<int>::iterator::difference_type result1;
- result1 = count_if(v1.begin(), v1.end(), greater10);
- cout << "The number of elements in v1 greater than 10 is: "
- << result1 << "." << endl;
- }
Output
v1 = ( 10 20 10 40 10 )
The number of elements in v1 greater than 10 is: 2.
Requirements
Header: <algorithm>