读入了一系列string对象,如何寻找其中重复次数最多的单词并将其重复次数记下?
读入了一系列string对象,如何寻找其中重复次数最多的单词并将其重复次数记下?
为什么不能?
分析出每个单词。之后
strstr,查找第一个,在跳到这个的末尾,再strstr~~~
程序代码:#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;
}
