C++ function 类模板

#include <iostream>
#include <future>
#include <vector>
#include <map>
#include <functional>
#include <string>

using namespace std;


int echovalue(int v)
{
	cout << "int echovalue(int v) :"<< v << endl;
	return v;
}

class biggerthenzero
{
public:
	int operator() (int v)
	{
		cout << "int operator() ():"<< v << endl;

		return v;
	}
};

int main()
{
	map<string, int(*)(int)> myoper;
	myoper.insert({"ev",echovalue});

	biggerthenzero obj;


	function<int(int)> f1 = echovalue;
	function<int(int)> f2 = obj;
	function<int(int)> f3 = biggerthenzero();

	f1(5);
	f2(3);
	f3(0);

	map<string, function<int(int)>> myoper1 = 
	{
		{"ev",echovalue},
		{"bt",obj},
		{"bt2",biggerthenzero()},
	};

	myoper1["ev"](12);
	myoper1["bt"](12);
	myoper1["bt2"](12);

	return 0;
}

posted @ 2022-11-21 18:33  repinkply  阅读(15)  评论(0)    收藏  举报