C++ Primer第四版习题6.20

来源:互联网 发布:unblockcn mac打不开 编辑:程序博客网 时间:2024/06/06 13:01

#include<iostream>
#include<string>
#include<vector>

using namespace std;

//编写程序从标准输入读入一系列string对象,直到同一个单词连续出现两次,或者所有的单词都已读完,才结束读取。请使用while循环,
//每次循环读入一个单词。如果连续出现相同的单词,便以break语句结束循环,此时,请输出这个重复出现的单词;否则输出没有任何单词
//连续重复出现的信息

bool choose(vector<string> vect,string currWord)
{
       for(unsigned int i = 0;i <(vect.size()-1);i++)
       {
            if(currWord == vect[i])
                 return true;
      }
      return false;
}

int main()
{
    vector<string> vect;
    string currWord;
    int k = 0;
    bool chos = false;
    cout<<"Enter some words:"<<endl;
    while(cin >> currWord)
    {
          vect.push_back(currWord);
          chos = choose(vect,currWord);
          if(chos && k>0)
                break;
          k++;
   }
   if(chos == true)
         cout<<"The repeated word:"<<currWord<<endl; 
   else
        cout<<"There is no repeated word."<<endl;
 
 return 0;
}

原创粉丝点击