本文记录我对c11当中有关函数特性的学习。主要是functor,std::function,std::bind
functor
定义
其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了。
说说我的理解,我认为引入functor的目的是为了赋予函数变量语义上的意义。其实对于functor做的事情,我在c里面也可以做,比如定义typedef void(*callback)(int,int); f( … , callback add);但是,引入functor之后,函数就可以很自然的成为一个变量,既然是变量,那么就可以作为参数进行传递。优点
在我们写代码时有时会发现有些功能实现的代码,会不断的在不同的成员函数中用到,但是又不好将这些代码独立出来成为一个类的一个成员函数。但是又很想复用这些代码。写一个公共的函数,可以,这是一个解决方法,不过函数用到的一些变量,就可能成为公共的全局变量,再说为了复用这么一片代码,就要单立出一个函数,也不是很好维护。这时就可以用仿函数了,写一个简单类,除了那些维护一个类的成员函数外,就只是实现一个operator(),在类实例化时,就将要用的,非参数的元素传入类中。这样就免去了对一些公共变量的全局化的维护了。
从这段分析中看出来,仿函数的优点是可以避免写成公共函数之后维护全局变量的代价。
// functor
struct CmpPair{
bool operator()( pair<int,int>& lhs, pair<int,int>& rhs ){
return rhs.second < lhs.second;
}
};
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
vector<int> ret;
int sz = nums.size();
if(!sz) return ret;
map<int, int> counter;
for(int i = 0; i < sz; ++i){
++counter[nums[i]];
}
vector< pair<int,int> > record( counter.begin(), counter.end() );
sort( record.begin(), record.end(), CmpPair() );
for(int i = 0; i < k; ++i){
ret.push_back( record[i].first );
}
return ret;
}
};
std::function
- 定义
Function wrapper.
Class that can wrap any kind of callable element (such as functions and function objects) into a copyable object, and whose type depends solely on its call signature (and not on the callable element type itself).
我自己的理解,英文定义已经说的很清楚了,我认为std::function和functor具有同样的工作方式,就是赋予函数变量的语义。好处就是方便把函数参数传递。
function< T >, T 是函数的标签
#include <iostream>
#include <functional>
int add_int( int a, int b ){ return a+b;}
double add_double( double a, double b ){ return a+b; }
struct Less{
bool operator()(int lhs, int rhs){
return lhs < rhs;
}
};
int main(){
std::function<int(int,int)> fn1 = add_int;
std::function<double(double, double)> fn2 = add_double;
std::function<bool(int,int)> fn3 = Less(); // function object
std::cout << fn1( 2, 3 ) << std::endl;
std::cout << fn2( 1.1, 2.3 ) << std::endl;
std::cout << fn3(1,2) << std::endl;
return 0;
}
std::bind
- 定义
Bind function arguments.
Returns a function object based on fn, but with its arguments bound to args.
定义说的很清楚,但是这个要看例子才可以明白。
#include <iostream>
#include <functional>
void test_case( int a, char b, double c ){
std::cout << a << " " << b << " " << c << std::endl;
}
int main(){
auto bind_func1 = std::bind( test_case, std::placeholders::_1, 'a', 0.1 );
bind_func1(10);
auto bind_func2 = std::bind( test_case, std::placeholders::_2, std::placeholders::_1, 0.3 );
bind_func2( 'a', 10 );
return 0;
}
/*
10 a 0.1
10 a 0.3
*/
从上面的形式我们可以看出,std::bind从理论上来说和前两者一样,它是生成一个functor,相当于赋予函数变量的语义。但是它可以绑定函数的参数,相当于生成自己定制的functor。上面的std::placeholders::_1,代表的是占位符号,这个说明真实调用函数参数过程中,_1代表第一个参数,placeholders::_1目前所在的位置,就是1号参数需要传递进来的位置。
问题是看了上面的代码,觉得这个例子仿佛是为了举例子而举例子,没有什么实际的用途,下面说一个实际的用途。
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
int main( void ){
std::vector<int> arr;
for( int i = 0; i < 10; ++i ){
arr.push_back(i);
}
/* find the element which is less than bound */
const int bound = 3;
auto less_functor = std::bind( std::less<int>(), std::placeholders::_1, bound );
int res = count_if( arr.begin(), arr.end(), less_functor);
std::cout << res << std::endl;
/* find the element which is greater than bound */
const int upper_bound = 8;
auto less_functor1 = std::bind( std::less<int>(), upper_bound, std::placeholders::_1 );
res = count_if( arr.begin(), arr.end(), less_functor1 );
std::cout << res << std::endl;
return 0;
}