读入了一系列string对象,如何寻找其中重复次数最多的单词并将其重复次数记下?
读入了一系列string对象,如何寻找其中重复次数最多的单词并将其重复次数记下?
2014-01-15 15:49
2014-01-15 16:55
2014-01-15 19:48
程序代码:#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<string,size_t> wordnums;
for( string str; cin>>str; )
++wordnums[str];
map<string,size_t>::const_iterator maxitor = wordnums.begin();
for( map<string,size_t>::const_iterator itor=wordnums.begin(); itor!=wordnums.end(); ++itor )
{
if( itor->second > maxitor->second )
maxitor = itor;
}
cout << maxitor->first << endl;
return 0;
}
2014-01-16 11:54

2014-01-17 03:54
2014-01-18 20:23
2014-01-19 14:59
2014-01-20 07:12

2014-01-20 08:37