map的使用,用完一次记得清空,不然会WA
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<string,int> m;
map<string,int>::iterator it;
int n;
cin >> n;
while (n != 0)
{
string s = "0";
string c = "0";
int a = -10;
for (int i=0; i<n; i++)
{
cin >> s;
m[s] += 1;
}
for (it=m.begin(); it!=m.end(); it++)
{
if (it->second > a)
{
a = it->second;
c = it->first;
}
}
cout << c << endl;
cin >> n;
m.clear();
}
}
本文深入探讨了C++编程中map数据结构的使用方法,并强调了每次操作后的清空策略以避免潜在的运行时错误(WA)。通过实例展示了如何高效地进行数据的插入、遍历及更新,确保程序的稳定性和性能。
1006

被折叠的 条评论
为什么被折叠?



