用 Map 扩展switch

#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)