---
title: "用 Map 扩展switch"
author: "mywebcode"
date: "2014-04-17T15:30:00Z"
tags: []
slug: "map-switch"
draft: false
lastmod: "2014-04-30T09:13:55Z"
id: 2058242
url: "https://mywebcode.iteye.com/blog/2058242"
---

```
#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;
}
```