primer C++ 第五章 5.4.1节练习5.14题
#include <iostream>
#include <string> //字符串头文件 //加上.h后就会报错误 没有与这些操作数匹配的">>"运算符
#include <vector> //容器头文件
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
int main()
{
cout << "Please input your words: " << endl;
string word, s1, s2;
vector <string> str1; //声明容器
//vector <string> ::iterator st1; //声明迭代器
int cnt = 0, repeated_t = 0, num = 0;
while (getline(cin, word)) {
if (!word.empty()) {
str1.push_back(word);
break;
} else {
cout << "不能为空,请重新输入";
continue;
}
}
for (auto st1 = word.begin() ; st1 < word.end(); st1++) {
if(!isspace(*st1)) {
s1 += *st1;
} else {
str1.push_back(s1);
s1 = "";
cnt++;
}
}
for (auto st2 = str1.begin(); st2+1 != str1.end(); st2++) {
if (*st2 == *(st2 + 1)) {
repeated_t++;
if (repeated_t >= num) {
num = repeated_t;
s2 = *(st2+1);
}
} else if(*st2 != *(st2 + 1)) {
repeated_t =0;
}
}
cout << s2 << "总数:" << num + 1;
}

浙公网安备 33010602011771号