⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exp20_2.cpp

📁 高等教育出版社出版的C++程序设计同步实验范例 希望对用这本教材得同学有点帮助
💻 CPP
字号:
/*多重映射multimap和映射map关联容器的使用。多重映射和映射关联容器类用于快速存储和
读取关键字与相关值(关键字/数值对,key/value pair),相关值可以是指针,指向相关资料,
实现索引查找。例如保存学生的简明资料,要求按学号排序,使用映射关联容器(因为不会重号)
是最合适的。如用姓名排序,因姓名可能重复,使用多重映射更为合适。使用时要用头文件<set>。
map和multimap将key/value pair当做元素进行管理,并根据key的排序准则自动将元素排序。
首先看演示程序。
*/
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;	//	C++命名空间域
//	定义整数和字符串的多重映射容器模板类,并以less<int>为排序准则
typedef multimap<int,string,less<int> >INT2STRING;		
int main() {
	INT2STRING theMap;							//	定义多重映容器射模板对象
	INT2STRING::iterator theIterator;					//	定义迭代器
	string theString="";
	int index;
	theMap.insert(INT2STRING::value_type(0,"Zero"));	//	初始化数据
	theMap.insert(INT2STRING::value_type(1,"One"));
	theMap.insert(INT2STRING::value_type(2,"Two"));
	theMap.insert(INT2STRING::value_type(3,"Three"));
	theMap.insert(INT2STRING::value_type(4,"Four"));
	theMap.insert(INT2STRING::value_type(5,"Five"));
	theMap.insert(INT2STRING::value_type(6,"Six"));
	theMap.insert(INT2STRING::value_type(7,"Seven"));
	theMap.insert(INT2STRING::value_type(8,"Eight"));
	theMap.insert(INT2STRING::value_type(9,"Nine"));
	for(;;) {
		cout<<"Enter \"q\" to quit, or enter a number:";
		cin>>theString;						//	输入字符串
		if(theString=="q")
			break;
		for(index = 0; index<(signed)theString.length();index++)	{
			theIterator = theMap.find(theString[index]-'0');	//	在容器内查找
			if(theIterator !=theMap.end())				//	如果是数字字符
				cout<<(*theIterator ).second<<" ";
			else 									//	其它字符
				cout<<"[err]";
		}
		cout<<endl;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -