包装器是在functional的头文件中。看一下代码:
#include<functional>
template<class F,class T>
T useF(F f, T x)
{
static int count = 0;
cout << "count:" << ++count << endl;
cout << "count:" << &count << endl;
return f(x);
}
double f(double i)
{
return i / 2;
}
struct Funtor
{
double operator()(double d)
{
return d / 3;
}
};
int main()
{
cout << useF(f, 11.11) << endl;
cout << useF(Funtor(), 11.11) << endl;
cout << useF([](double d)->double {return d / 4; }, 11.11) << endl;
return 0;
}
这里的f被实例化了3份,那么能不能实例话成一份。就需要引入包装器。
#include<functional>
template<class F,class T>
T useF(F f, T x)
{
static int count = 0;
cout << "count:" << ++count << endl;
cout << "count:" << &count << endl;
return f(x);
}
double f(double i)
{
return i / 2;
}
struct Funtor
{
double operator()(double d)
{
return d / 3;
}
};
int main()
{
/*cout << useF(f, 11.11) << endl;
cout << useF(Funtor(), 11.11) << endl;
cout << useF([](double d)->double {return d / 4; }, 11.11) << endl;*/
function<double(double)> f1 = f; //包装普通函数
cout << f1(11.11) << endl;;
function<double(double)> f2 = Funtor(); //包装仿函数
cout << f2(11.11) << endl;
function<double(double)> f3 = [](double d)->double {return d / 4; }; //包装lamdba表达式
cout << f3(11.11) << endl;
return 0;
}
#include<iostream>
using namespace std;
#include<functional>
int add(int x, int y)
{
return x + y;
}
struct Add
{
int operator()(int x, int y)
{
return x + y;
}
};
class Puls
{
public:
static int pulsi(int x, int y)
{
return x + y;
}
double pulsd(double x, double y)
{
return x + y;
}
};
int main()
{
function<int(int, int)> f1 = add; //包装普通函数
cout << f1(1, 2) << endl;
function<int(int, int)> f2 = Add(); //包装仿函数
cout << f2(1, 2) << endl;
function<int(int, int)> f3 = &Puls::pulsi; //包装成员函数可以不加&但是最好加上
cout << f3(1, 2) << endl;
function<int(Puls,int, int)> f4 = &Puls::pulsd; //包装非静态函数需要增加参数,但是可以通过band来取消这个参数
cout << f4(Puls(), 1, 2) << endl;
}
在有点时候包装器的作用还是非常有用的,我看一个题
笨方法:
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for(const auto& e: tokens)
{
if((e != "+" && e != "-" && e != "*" && e != "/"))
{
st.push(stoi(e));
}
else
{
int right = st.top();
st.pop();
int left = st.top();
st.pop();
switch(e[0])
{
case '+':
st.push(right + left);
break;
case '-':
st.push(left - right);
break;
case '*':
st.push(left * right);
break;
case '/':
st.push(left / right);
break;
}
}
}
return st.top();
}
};
class Solution {
public:
int evalRPN(vector<string>& tokens) {
unordered_map<string,function<int(int,int)>> mp;
mp["+"] = [](int a, int b )->int{ return a + b; };
mp["-"] = [](int a, int b )->int{ return a - b; };
mp["*"] = [](int a, int b )->int{ return a * b; };
mp["/"] = [](int a, int b )->int{ return a / b; };
stack<int> st;
for(const auto &e : tokens)
{
if(mp.find(e) == mp.end())
{
st.push(stoi(e));
}
else
{
int right = st.top();
st.pop();
int left = st.top();
st.pop();
st.push(mp[e](left,right)); //调用包装器的方法
}
}
return st.top();
}
};
用包装器一个符号就表示一种方法,游戏开发也是类似,一个按键表示一种状态。这样方便很多。
functional绑定。
#include<functional>
int add(int a, int b)
{
return a - b;
}
struct Add
{
int add(int a, int b)
{
return a - b;
}
};
int main()
{
//调增参数位置
function<int(int, int)> f1 = bind(add,placeholders::_1, placeholders::_2);//不绑定
cout << f1(10, 3) << endl;
function<int(int, int)> f2 = bind(add, placeholders::_2, placeholders::_1);//交换了参数位置
cout << f2(10, 3) << endl;
//调增参数个数
function<int(Add,int, int)>f3 = &Add::add;
cout << f3(Add(),10, 3) << endl;
function<int(int, int)>f4 = bind(&Add::add, Add(), placeholders::_1, placeholders::_2);
cout << f4(10, 3) << endl;
}