Note that for the general C + + 98 standard compiler, container generic templates do not support directly storing function pointers. A typedef is required to rename the function pointer.
For example, a function pointer that has a void return value parameter is void, which requires
void (*test) (void);
In this way, test can be added to the container as a function pointer template type.
map<int, test> testmap;pair<int,test> p1 = Make_pair (1, displayone);
The following sample code is a simple demonstration:
#include <iostream>#include<string>#include<map>using namespaceStd;typedefvoid(*test) (void);
voidDisplayone () {cout<<" One"<<Endl;}
voidDisplaytwo () {cout<<" Both"<<Endl;}
intMainintargcChar*argv[]) {Map<int, test>TestMap; Pair<int,test> P1 = Make_pair (1, Displayone); Pair<int,test> P2 = Make_pair (2, Displaytwo); Testmap.insert (p1); Testmap.insert (p2); testmap[1](); (*testmap[2])(); System ("Pause");}
C + + holds function pointers in containers