#include <vector>
#include <map>
#include <string>
using namespace std;
// 声明一个函数指针
int(*pFunc)(int);
int func1(int nIn){return nIn + 1;}
int func2(int nIn){return nIn + 20;}
typedef int(*pInt)(int);//定义别名才能放在vector中
void main()
{
pFunc = func1;// 把函数名赋给函数指针
int n = pFunc(1);
pFunc = &func2;
n = pFunc(1);
//vector<int(*pFun)(int)> v_pFunc;//不能这样定义
//
vector<pInt> v_pInt;
v_pInt.push_back(func1);
v_pInt.push_back(func2);
int i = v_pInt[0](2);
i = v_pInt[1](2);
//
map<string,pInt> map_pInt;
map_pInt.insert(pair<string,pInt>("key1",func1));
map_pInt.insert(pair<string,pInt>("key2",func2));
int j = map_pInt["key1"](3);
j = map_pInt["key2"](3);
}
20121028更正vector可以直接放函数指针类型,没理解到位。
vector<int(*)(int)> v_pFunc;
v_pFunc.push_back(func1);
v_pFunc.push_back(func2);
int k = v_pFunc[0](5);
k = v_pFunc[1](5);
url:http://greatverve.cnblogs.com/archive/2012/10/27/vector-ptr.html
原文链接: https://www.cnblogs.com/greatverve/archive/2012/10/27/vector-ptr.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/67199
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!