读书人

【STL】地图

发布时间: 2013-10-01 12:15:56 作者: rapoo

【STL】map
1.概述


map是一种关联式容器(associative container),键值对<key, value>序列。它提供基于key的快速检索能力,在一个map中key值是唯一的。map提供双向迭代器,即有从前往后的(iterator),也有从后往前的(reverse_iterator)。


泛型原型:template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>
第一个参数Key是关键字类型 ;
第二个参数T是值类型 ;
第三个参数Compare是比较函数(仿函数);
第四个参数是内存配置对象。


2. 基本操作


.insert( )插入<key, value>对。

.find(key)查找map中key,若能查找到,返回iterator;若没找到,返回map::end。

.begin( ), .end( )表示迭代器的开始与结束。

.earse( )从map中删除<key, value>对。

.clear()相当于 .erase(.begin( ), .end( ));


3. Referrence


[1] cplusplus,http://www.cplusplus.com/reference/map/map/

[2] 吴秦,STL之Map。

[3] wuqifu,C++ STL map::insert小结。


4.问题


4.1 POJ 1002


题目大意:根据映射规则,求重复的电话号码。


用map<string,int>记录每种电话号码的个数。


源代码:

1002Accepted5192K1329MSC++829B2013-09-25 20:50:49

2491Accepted320K329MSC++697B2013-09-26 10:54:13

2643Accepted264K16MSC++792B2013-09-26 16:01:51

2503Accepted9620K1204MSC++450B2013-09-27 17:15:03

#include<iostream>#include<map>#include<string>using namespace std;map<string,string>dictionary;int main(){string str1,str2;char ch;while(1){cin>>str1>>str2;dictionary[str2]=str1;getchar();ch=getchar();if(ch=='\n')break;ungetc(ch,stdin);}while(cin>>str1){if(dictionary.find(str1)!=dictionary.end())cout<<dictionary[str1]<<endl;elsecout<<"eh\n";}return 0;}


读书人网 >其他相关

热点推荐