您的位置:首页 > 其它

stl库之map、multimap

2013-11-27 15:53 302 查看
1、映射和多重映基于某一类型Key的键集的存在,提供对T类型的数据进行快速和高效的检索。
2、对map而言,键只是指存储在容器中的某一成员。
3、Map不支持副本键,multimap支持副本键。
4、Map和multimap对象包涵了键和各个键有关的值,键和值的数据类型是不相同的,这与set不同。
5、set中的key和value是Key类型的,而map中的key和value是一个pair结构中的两个分量。
6、Map支持下表运算符operator[],用访问普通数组的方式访问map,不过下标为map的键。
7、在multimap中一个键可以对应多个不同的值。
实例:
#include "stdafx.h"
#include <map>
#include <string>
#include <iostream>
using namespace std;

struct classcomp {  
  bool operator() (const char& lhs, const char& rhs) const  
    {return lhs<rhs;}  
};  

void showmap(string infos,map<char,int> map1);
void showmap(string infos,map<char,int> map1){
	infos=infos+"mapItems:";
	cout<<endl<<infos<<endl;  
	for(map<char,int>::iterator mapIter=map1.begin();mapIter!=map1.end(); ++mapIter)  
	{
		char c=(*mapIter).first;
		cout<<" "<<c<<": "<<(*mapIter).second;
	}
}

void showMultimap(string infos,multimap <string,string> map1);
void showMultimap(string infos,multimap <string,string> map1){
	infos=infos+"mapItems:";
	cout<<endl<<infos<<endl;  
	for(multimap<string,string>::const_iterator p=map1.begin();p!=map1.end(); ++p)  
	{
		string s=(*p).first;
		cout<<" "<<s<<": "<<(*p).second<<endl;
	}
}

int _tmain(int argc, _TCHAR* argv[])  
{
	//构造函数
	map <char,int> map0;  //char 是键的类型,int是值的类型
	map0['c']=3;
	map0['d']=4;
	map0['a']=1;
	map0['b']=2;
	map0['c']=99;
	map0['d']=88;
	map <char,int> map1(map0.begin(), map0.end());    
	map <char,int,classcomp> map3; 
	map <char,int> map4(map1); 

	multimap <string,string> multimap5;

	typedef multimap<string,string,less<string> >::value_type vt;
    typedef string s;
    multimap5.insert(vt(s("Tom "),s("is a student")));
    multimap5.insert(vt(s("Tom "),s("is a boy")));
    multimap5.insert(vt(s("Tom "),s("is a bad boy of blue!")));
    multimap5.insert(vt(s("Jerry "),s("is a student")));
    multimap5.insert(vt(s("Jerry "),s("is a beatutiful girl")));
    multimap5.insert(vt(s("DJ "),s("is a student")));

	showMultimap("原multimap5  ",multimap5);
	multimap<string,string,less<string> >::iterator p;
	 cout<<"multimap5.find(s(\"Jerry \"))"<<endl;
	p=multimap5.find(s("Jerry "));
    while((*p).first=="Jerry ")
    { 
        cout<<(*p).first<<(*p).second<<endl;
        ++p;
    }  

	showmap("原map1  ",map1);

	map1.insert(map<char,int>::value_type('f',11)); //map<>::value_type()
	map1.insert(pair<char,int>('e',26)); //pair
	map1.insert(make_pair('g',100)); //make_pair()
	showmap("insert(++map1.begin(),5)  ",map1);

	map<char,int,less<char> >::const_iterator ptr;
	ptr=map1.find('d');
	cout<<endl<<"find('d')";
	cout<<endl<<(*ptr).first<<':'<<(*ptr).second;

	//删除成员
	map1.erase(ptr);
	showmap("erase(ptr)  ",map1);
	map1.erase('c');
	showmap("erase('c')  ",map1);

	//删除成员
	map1.erase(++map1.begin(),--map1.end());
	showmap("erase(++map1.begin(),--map1.end())  ",map1);

	//显示状态信息
	cout<<endl<<"map1.size(): "<<map1.size()<<endl;//打印成员个数
	cout<<"map1.empty(): "<<map1.empty();//是否为空

	//清除
	map1.clear();
	showmap("clear()  ",map1);
	cout<<endl<<"map1.size(): "<<map1.size()<<endl;//打印成员个数
	cout<<"map1.empty(): "<<map1.empty();//是否为空
	getchar();
	return 0;
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: