c++中的关联容器

c++中的关联容器主要是map、set,已经multimap、multiset。

为了讲map,得先将pair类型:pair就是一个两个类型的组合,比如一个人的学号就可以是pair<int,string>,其中的int是学号,string是名字。

map就是一个容器,里面装的就是若干个pair。每个pair的第一个元素,称为键(注意键的类型必须支持小于(<)操作!),第二个元素,称为值。对于普通的map,每个键都对应一个值。这样的看起来,键类似于数组的下标,而值类似于数组的值。map类定义了3种类型,分别为key_type、mapped_type、以及vaule_type。他们分别表示了键的类型、值的类型,以及一个pair类型,pair的第一个元素是键,第二个元素是值。

首先讲讲如何给imap添加元素:有两种的方法,使用键(下标)或者使用insert

使用下标这种方法与vector等类型的容器相矛盾。如果是一个空的vector,则不能使用下标直接访问,必须pusn_back进元素才行,直接访问会报错。而对于map如果没有这个键(下标),则会自动的向map中添加这个键,值为0。下标操作的返回值,就是这个键关联的值。利用这个性质,我们可以很方便的完成统计的功能,举个例子:

[cpp]  view plain copy
  1. int main()  
  2. {  
  3.     string str;  
  4.     map<string,int> wordCount;  
  5.     while(cin>>str)  
  6.     {  
  7.         ++wordCount[str];  
  8.     }  
  9.       
  10.     map<string,int>::iterator it_map = wordCount.begin();  
  11.     cout<<"word"<<"\t\t"<<"count"<<endl;  
  12.     for(;it_map != wordCount.end();++it_map)  
  13.         cout<<it_map->first<<"\t\t"<<it_map->second<<endl;  
  14.     return 0;  
  15.   
  16. }  


 

使用时如果键不存在,则赋值为0,然后对其自增。即变成了1。

insert方法有多个重载函数,表明你插入的是一个pair,还是一对迭代器指明的若干个pair,还是插入一个pair到指定的位置,但通常使用的插入一个pair,这个函数最关键是它的返回值。这个返回值也是一个pair,pair的第一个元素是指向该map类型的迭代器,另一个是bool类型的变量,表明插入成功与否。可以用insert重写上面的程序:

 

[cpp]  view plain copy
  1. int main()  
  2. {  
  3.     string str;  
  4.     map<string,int> wordCount;  
  5.     while(cin>>str)  
  6.     {  
  7.         //对于每个单词,都尝试去插入它  
  8.         pair<map<string,int>::iterator,bool>ret = wordCount.insert(make_pair(str,1));  
  9.         //通过检测返回值来判断插入是否成功  
  10.         if(!ret.second)  
  11.             //插入失败表明map中有这个单词,只需要把对应键的值自增即可  
  12.             ++ret.first->second;  
  13.     }  
  14.       
  15.   
  16.     map<string,int>::iterator it_map = wordCount.begin();  
  17.     cout<<"word"<<"\t\t"<<"count"<<endl;  
  18.     for(;it_map != wordCount.end();++it_map)  
  19.         cout<<it_map->first<<"\t\t"<<it_map->second<<endl;  
  20.     return 0;  
  21.   
  22. }  


那么如何读取map的元素呢?虽然我们也可以使用键来读取,但是潜在的问题是如果该键不存在,就会自动创建,这个特点并不是我们所希望的。我们可以通过count或者find函数来查找某个键是否存在。这两个函数的区别在于count返回的是出现的次数(对于map只能为0或者1),而find则返回的是指向该键的迭代器(如果没有找到,返回超末端迭代器:.end())。这意味着如果你是为为了统计是否存在,使用count就可以了,如果你找到某个元素并且还想使用它,那么find会比较合适。

删除元素使用使用erase操作,这与顺序容器差别不大。但是要注意的是,关联容器的顺序是按照“键”排列的。

下面通过一个小程序来说明:

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <map>  
  3. #include <string>  
  4. #include <vector>  
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     string str;  
  10.     map<string,int> wordCount;  
  11.     while(cin>>str)  
  12.     {  
  13.         //对于每个单词,都尝试去插入它  
  14.         pair<map<string,int>::iterator,bool>ret = wordCount.insert(make_pair(str,1));  
  15.         //通过检测返回值来判断插入是否成功  
  16.         if(!ret.second)  
  17.             //插入失败表明map中有这个单词,只需要把对应键的值自增即可  
  18.             ++ret.first->second;  
  19.     }  
  20.       
  21.   
  22.     map<string,int>::iterator it_map = wordCount.begin();  
  23.     cout<<"word"<<"\t\t"<<"count"<<endl;  
  24.     for(;it_map != wordCount.end();++it_map)  
  25.         cout<<it_map->first<<"\t\t"<<it_map->second<<endl;  
  26.   
  27.     //count方法:对于map,返回1或者0  
  28.     int cnt = 0;  
  29.     cnt = wordCount.count("bird");  
  30.     cout<<"cnt"<<cnt<<endl;  
  31.   
  32.     //find方法:返回的是指向键的迭代器  
  33.     int occurs = 0;  
  34.     map<string,int>::iterator it = wordCount.find("bird");  
  35.     if(it != wordCount.end())  
  36.         occurs = it->second;  
  37.     cout<<"occurs = "<<occurs<<endl;  
  38.   
  39.   
  40.     //删除元素  
  41.     int del;  
  42.     string s1 = "hate";  
  43.     //使用值删除  
  44.     del = wordCount.erase(s1);  
  45.     if(del)  
  46.         cout<<s1<<" has been removed! "<<endl;  
  47.     else  
  48.         cout<<"can't find the word! "<<endl;  
  49.   
  50.     //使用迭代器删除  
  51.     map<string,int>::iterator iter = wordCount.begin();  
  52.     wordCount.erase(iter);  
  53.   
  54.   
  55.     return 0;  
  56.   
  57. }  


 

说完了map,我们在看看set。set只有键,没有值,所以他的vaule_type不是pair类型,而是key_type类型。同样的,它也支持insert、find、count操作。map比较适合于储存键值对应的情况,而set适合于储存键,判断键在不在这个集合中,比如黑名单之类的。

前面两种关联容器的特点是,键与值的对应关系是唯一的。而multimap或者multiset则支持一对多的关系。而且对于相同的键,总是连续的存储。由于这种一对多关系,所以multimap和multiset支持一些map和set没有的操作。比如lower_bound、upper_bound以及equal_range.它们分别返回的是指向某个键的第一个元素,最后一个元素的下一个元素以及这连个元素组成的范围。看一个综合例子:

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <map>  
  3. #include <string>  
  4.   
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     multimap<string,string> authors;  
  10.     string author,work,searchItem;  
  11.   
  12.     //建立作者及其作品的容器  
  13.     do  
  14.     {  
  15.         cout<<"enter authors name"<<endl;  
  16.         cin>>author;  
  17.         if(!cin)  
  18.             break;  
  19.         cout<<"enter authors works"<<endl;  
  20.         while(cin>>work)  
  21.             authors.insert(make_pair(author,work));  
  22.         cin.clear();  
  23.     }while(cin);  
  24.   
  25.     cin.clear();  
  26.     //删除元素  
  27.   
  28.     cout<<"who is the author that you want erase:"<<endl;  
  29.     cin>>searchItem;  
  30.     /* 
  31.     //使用erase删除:输出对应键的所有值 
  32.     multimap<string,string>::iterator iter = authors.find(searchItem); 
  33.     if(iter != authors.end()) 
  34.         authors.erase(searchItem); 
  35.     else 
  36.         cout<<"cannot find the author!"<<endl; 
  37.     */  
  38.   
  39.     //使用equal_range或得迭代器删除  
  40.     typedef multimap<string,string>::iterator itType;  
  41.     pair<itType,itType> pos = authors.equal_range(searchItem);  
  42.     if(pos.first != pos.second)  
  43.         authors.erase(pos.first,pos.second);  
  44.     else  
  45.         cout<<"can not find this author!"<<endl;  
  46.   
  47.   
  48.     //输出删除结果  
  49.     cout<<"author\t\twork:"<<endl;  
  50.     multimap<string,string>::iterator itbegin = authors.begin();  
  51.     for(;itbegin != authors.end();++itbegin)  
  52.         cout<<itbegin->first<<"\t\t"<<itbegin->second<<endl;  
  53.   
  54.     return 0;  
  55. }  

FROM:  http://blog.csdn.net/thefutureisour/article/details/7683656



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值