5.14 从标准输入中读取若干string对象并查找连续重复出现的单词,统计出现重复最大次数的单词,比如在how now now now brown cow cow中now连续出现3次
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
pair<string, int> max_duplicated;
int count = 0;
for (string curstr,prestr; cin>>curstr; prestr=curstr)
{
if (curstr == prestr)
++count;
else
count = 0;
if (count>max_duplicated.second)
max_duplicated = {prestr, count};
}
if (max_duplicated.first.empty())
cout<<"There's no duplicated string."<<endl;
else
cout<<"the word "<<max_duplicated.first<<" occurred "<<max_duplicated.second+1<<" times. "<<endl;
system("pause");
return 0;
}
或者
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string before,current,key;
int cnt=1;
vector<string> str;
vector<int> num;
while (cin >> current)
{
if (before.empty())
{
before = current;
}
else if (current == before)
{
++cnt;
}
else
{
str.push_back(before);
num.push_back(cnt);
before = current;
cnt = 1;
}
}
if (cnt != 1)
{
str.push_back(current);
num.push_back(cnt);
}
vector<string>::iterator iter;
vector<int>::iterator n;
for (iter=str.begin(),n=num.begin(); iter != str.end(); ++iter,++n)
{
cout << *iter << "occurred times" << *n <<endl;
}
system("pause");
return 0;
}
5.17 检验一个vector对象是否是另一个的前缀,挑出长度较短的那个比较元素内容即可,比如0,1,1,2和0,1,1,2,3,5,8返回为真
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool is_prefix(vector<int> const& avec, vector<int> const& bvec)
{
int small_size = avec.size()<bvec.size() ? avec.size() : bvec.size();
for(unsigned i = 0; i != small_size; ++i)
if(avec[i]!=bvec[i])
return false;
return true;
}
int main()
{
int a[4]={0,1,1,2};
int b[7]={0,1,1,2,3,5,8};
vector<int> vec1(a,a+4);
vector<int> vec2(b,b+7);
cout << ( is_prefix(vec1,vec2) ? "yes\n" : "no\n" );
system("pause");
return 0;
}
5.19 使用do while循环重复执行下述任务:首先提示用户输入两个string对象,然后挑出较短的那个并输出
#include <iostream>
#include <string>
using namespace std;
int main()
{
string rsp;
do {
cout << "Input two strings: ";
string str1, str2;
cin >> str1 >> str2;
cout << (str1<=str2 ? str1 : str2)
<< " is less than the other. " << "\n\n"
<< "More? Enter yes or no: ";
cin >> rsp;
} while (!rsp.empty() && tolower(rsp[0]) == 'y');
system("pause");
return 0;
}
5.20 从标准输入中读取string对象的序列直到连续出现两个相同的单词或者所有单词都读完为止(ctrl+Z)。使用while循环依次读取一个单词,当一个单词连续两次出现时使用break终止循环。最终输出连续重复出现的那个单词或者说明没有出现连续重复单词。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string curr, pre;
while (cin >> curr)
{
if (curr == pre)
break;
else
pre = curr;
}
if (cin.eof())
cout << "no word was repeated." << endl;
else
cout << curr << " occurs twice in succession." << endl;
system("pause");
return 0;
}
5.21 修改上题,使其找到的重复单词必须以大写字母开头
#include <iostream>
#include <string>
using namespace std;
int main()
{
string curr, pre;
bool no_twice = true;
while (cin >> curr)
{
if (isupper(curr[0]) && curr == pre)
{
cout << curr << ": occurs twice in succession." << endl;
no_twice = false;
break;
}
else
pre = curr;
}
if (no_twice)
cout << "no word was repeated." << endl;
system("pause");
return 0;
}