#include <iostream>
#include <iterator>
#include <string>
#include <map>
using namespace std;
// 函数指针定义
typedef void (* switchcallback)(void);
void displayA() {
cout << "Aaaaa" << endl;
}
void displayB() {
cout << "Bbbbb" << endl;
}
void displayC() {
cout << "Ccccc" << endl;
}
int main() {
map <string,switchcallback> stringMap;
stringMap.insert(pair<string,switchcallback>("Aaaaa",displayA));
stringMap.insert(pair<string,switchcallback>("Bbbbb",displayB));
stringMap.insert(pair<string,switchcallback>("Ccccc",displayC));
map<string,switchcallback>::iterator stringMapItr;
stringMapItr = stringMap.find("Ccccc");
stringMapItr->second();
return 0;
}
用 Map 扩展switch
最新推荐文章于 2023-10-26 00:35:01 发布
本文介绍了一种使用C++标准模板库(STL)中的map容器来实现字符串到函数指针映射的方法。通过具体的代码示例展示了如何插入不同的字符串及其对应的函数,并演示了如何通过查找特定的字符串来调用相应的函数。
32

被折叠的 条评论
为什么被折叠?



