c++新特性:function/bind
function
#include<function>
目的:
函数对象包装器
为函数提供了一种容器
支持4种函数的封装
- 普通函数
std::function<<int>> f = test; //test是函数名
- 匿名函数
std::function<<int>> f = [](int n)->int{
cout<< n <<endl;
return n;
}
- 类的成员函数
std::function<<CTest*,int>> f = &CTest::MyTest; //CTest是类名
- 仿函数
std::function<int(CTest*,int)> f = &CTest::operator(); //CTest中重载了()
bind
void add(int a,int b,int c)
{cout<<a<<b<<B}
auto a =std::bind(add,1,2,3);
a() = //123
auto a = std::bind(add,std::placeholders::_2,std::placeholders::_1,c)
a(1,2) //213