当前位置:C++技术网 > 资讯 > 利用STL中的map来实现字典查找

利用STL中的map来实现字典查找

更新时间:2016-03-21 22:16:44浏览次数:1+次

网站有两篇map的文章《用list来实现顺序map(不自动排序的map)》《标准模板库_map映照容器》,你可以看看。STL中的map,在数据结构中,我们有叫做映射,还有的叫做字典。下面,我们就来看看所谓的映射是怎么回事。
#include "iostream"
#include "windows.h"
#include "map"
#include "string"

using namespace std;

int main()
{
map<string,int> counts;
string words;

while (cin>>words)
{
++counts[words];//在STL中,对于string类型的变量输入,不容易结束输入,应当输入ctrl+z结束
}
for(map<string,int>::iterator iter(counts.begin()); iter!=counts.end(); ++iter)
{
cout<<iter->first<<'\t'<<iter->second<<'\n';
}
system("pause");
return 0;
}
下面看看执行结果: