原题是C++ Primer(4)上的习题6.12,稍微扩展了下功能,并且用了vector容器。书上的习题都不难,对我这个beginer来讲,在熟悉C++和养成良好编程习惯上有很大帮助。
//扩展功能:如果重复次数最多的相邻单词有多个,输出所有
#ifdef WIN32
#pragma warning (disable: 4514 4786)
#endif
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int max = 1;
int count = 0;
string str;
string pre;
vector<string> svec;
vector<string> mvec;
while (cin >> str)
svec.push_back(str);
for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
{
if (*iter == pre)
++count;
else
{
if (count >= max)
{
max = count;
mvec.push_back(pre);
}
count = 1;
}
pre = *iter;
}
for (iter = mvec.begin(); iter != mvec.end(); ++iter)
cout << *iter << endl;
cout << max << endl;
return 0;
}