注意,对一般c++ 98标准编译器而言,容器泛型模板是不支持直接存放函数指针的。需要typedef将函数指针重命名。
比如,一个void返回值参数也为void的函数指针,需要
typedef void(*test)(void);
这样,test就可以当做函数指针模板类型添加到容器当中了。
map<int, test> testMap;pair<,displayone);
以下示例代码简单的示范一下:
#include<iostream>#include<string>#include<map>using namespace std;typedef void(*test)(void);
void displayone(){ cout << "one" << endl;}
void displaytwo(){ cout << "two" << endl;}
int main(int argc, char* argv[]){ map<int, test> testMap; pair<,displayone); pair<,displaytwo); testMap.insert(p1); testMap.insert(p2); testMap[](); (*testMap[])(); system("pause");}