栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++11 包装器

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++11 包装器

包装器是在functional的头文件中。看一下代码:
#include

template
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

template
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()
{
	

	function f1 = f;   //包装普通函数
	cout << f1(11.11) << endl;;
	function f2 = Funtor();  //包装仿函数
	cout << f2(11.11) << endl;
	function f3 = [](double d)->double {return  d / 4; }; //包装lamdba表达式
	cout << f3(11.11) << endl;
	return 0;
}
#include
using namespace std;
#include
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 f1 = add;  //包装普通函数
	cout << f1(1, 2) << endl;
	function f2 = Add(); //包装仿函数
	cout << f2(1, 2) << endl;
	function f3 = &Puls::pulsi;  //包装成员函数可以不加&但是最好加上
	cout << f3(1, 2) << endl;

	function f4 = &Puls::pulsd;  //包装非静态函数需要增加参数,但是可以通过band来取消这个参数
	cout << f4(Puls(), 1, 2) << endl;
		 
}

 在有点时候包装器的作用还是非常有用的,我看一个题

逆波兰表达式求值

笨方法:
class Solution {
public:
    int evalRPN(vector& tokens) {
       stack 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& tokens) {
        unordered_map> 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 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
int add(int a, int b)
{
	return  a - b;
}
struct Add
{
	int add(int a, int b)
	{
		return  a - b;
	}
};
int main()
{
	//调增参数位置
	function f1 = bind(add,placeholders::_1, placeholders::_2);//不绑定
	cout << f1(10, 3) << endl;
	function f2 = bind(add, placeholders::_2, placeholders::_1);//交换了参数位置
	cout << f2(10, 3) << endl;
	//调增参数个数
	functionf3 = &Add::add;
	cout << f3(Add(),10, 3) << endl;
	functionf4 = bind(&Add::add, Add(), placeholders::_1, placeholders::_2);
	cout << f4(10, 3) << endl;
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1010261.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号