207页习题6.12读入string,输出连续重复单词次数

//为了方便,要包含的文件会不同,但我没有删
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
#include <cstring>   //和C语言的string.h其实是一个版本

using std::bitset;
using std::vector;
using std::cin;
using std::endl;
using std::cout;
using std::string;

//---------------------------------------------------------------------------

    int main()
    {
  vector<string> strVec;

  //为string类vector填值-------------------------------------
  string str;
  while(cin>>str)
  {
   strVec.push_back (str);
  
  }


//找连续重复的词
  int cnt=1,n=1;                                    //cnt初始为1,是因为任何词一开始都有1个,一但重复便为2了
  string strMost;
 for(vector<string>::iterator ite=strVec.begin ();ite!=strVec.end ();++ite)
  {
   while(ite+1!=strVec.end ()&&*ite==*(ite+1))   //由于在空指针上解引用是错误的,所以必须加上 ite+1!=strVec.end()并且放在左边,保证末端指针不会被解引用
   { ++cnt;
    
     ++ite;
   }
  
   if(cnt>n)
  {
   n=cnt;
   strMost=*ite;
  }
  
   cnt=1;
  }

  //输出无重复或重复信息
  if(n!=0)
 cout<<"连续重复次数最多的词是"<<strMost<<"/40"<<"共出现了"<<n<<"次"<<endl;
  else
   cout<<"无连续重复单词"<<endl;

 
d        return 0;
    }

 

 /*
   207页习题6.12读入string,输出连续重复单词次数
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值