Map 简介:
map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。
Map的功能
1.自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
2.根据key值快速查找记录,查找的复杂度基本是 Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
3.快速插入Key - Value 记录。
4.快速删除记录
5.根据Key 修改value记录。
6.遍历所有记录。
Map 迭代器: //常用于遍历操作
map<int,string>::iteratoriter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<””<<iter->second<<end;
}
下面是该题代码
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string,int> cat;
string str;
int N;
while(cin >> N && N)
{
cat.clear();
for(int i = 0;i < N;i ++)
{
cin >> str;
cat[str] ++;
}
map<string,int>::iterator ite;
int maxnumber = 0; string maxcolor;
for(ite = cat.begin();ite != cat.end();ite ++)
if(ite -> second > maxnumber){
maxnumber = ite -> second;
maxcolor = ite -> first;
}
cout << maxcolor << endl;
}
return 0;
}