C++Primer第五版 5.4.1节练习

C++Primer第五版 5.4.1节练习

练习5.14:编写一段程序,从标准输入中读取若干string对象并查找连续重复出现的单词。所谓连续重复出现的意思是:一个单词后面紧跟着这个单词本身。要求记录连续出现的最大次数以及对应的单词。如果这样的单词存在,输出重复出现的最大次数。如果不存在,输出一条信息说明任何单词都没有连续出现过。例如,如果输入是
how now now now brown cow cow
那么输出应该表明单词now连续出现了3次

#include <iostream>
#include <string>
#include <vector>
using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::cin;

int main(void)
{
	vector<string> list;
	string str;
	unsigned Cnt=0,Cnt2=0;
	while(cin>>str)
		list.push_back(str);
	list.push_back("fdfdl2;3d"); //循环需要
	auto beg=list.begin();
	string word=*beg,word2;
	while(beg!=list.end())
	{
		if(word==*beg)
			++Cnt;
		else
		{
			if(Cnt2<=Cnt)
			{
				Cnt2=Cnt;
				word2=word;
			}
			Cnt=0;
			word=*beg;
			continue;
		}
		++beg;
	}
	if(Cnt2<2)
		cout<<"任何单词都没有连续出现过"<<endl;
	else
		cout<<"连续最多的单词"<<word2<<"连续出现了"<<Cnt2<<"次"<<endl;
		
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值