请教一个关于字符串输入,在某种条件下结束输入的问题

ysysbaobei 2008-10-29 12:56:04
问题:
编写程序从标准输入读入一系列string对象,直到同一个单词连续出现2次,或者所有的单词都已读完,才结束读取。
请使用while循环,每次循环读入一个单词。如果连续出现相同的单词,便以break语句结束循环,此时,请输出这个重复出现的单词;否则输出没有任何单词连续重复出现的信息。
...全文
116 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysysbaobei 2008-10-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ysysbaobei 的回复:]
引用 2 楼 hhyttppd 的回复:
C/C++ code if(all_words.size() > 1 && input.compare(all_words.back()) == 0)
{
cout < < " it's repeat... which is " < < input < < endl;
break;
}

all_words.size() > 1 应该为: all_words.size() > 0
不然一个字母的单词重复的情况,不能正确执行
[/Quote]
写错了,是第一个单词和第2个单词重复的情况,程序检测不出来
ysysbaobei 2008-10-29
  • 打赏
  • 举报
回复
问题基本解决,结贴了,谢谢
ysysbaobei 2008-10-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hhyttppd 的回复:]
C/C++ code if(all_words.size() > 1 && input.compare(all_words.back()) == 0)
{
cout << " it's repeat... which is " << input << endl;
break;
}
[/Quote]
all_words.size() > 1 应该为: all_words.size() > 0
不然一个字母的单词重复的情况,不能正确执行
liumingrong 2008-10-29
  • 打赏
  • 举报
回复

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

typedef vector<string> SIMPLE_WORDS;

int main()
{
SIMPLE_WORDS all_words;

string input;

while(cin >> input)
{
if(!all_words.empty() && (all_words.back() == input))
{
cout << "repeated word:" << input << endl;
return 0;
}

all_words.push_back(input);
}

copy(all_words.begin(), all_words.end(), ostream_iterator<string>(cout,"\n"));
return 0;
}
ysysbaobei 2008-10-29
  • 打赏
  • 举报
回复
程序测试过没?
liumingrong 2008-10-29
  • 打赏
  • 举报
回复

if(find(all_words.begin(), all_words.end(), input) != all_words.end())
===> if(!all_words.empty() && (all_words.back() == input))
[Quote=引用 1 楼 hhyttppd 的回复:]
C/C++ code
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

typedef vector<string> SIMPLE_WORDS;

int main()
{
SIMPLE_WORDS all_words;

string input;

while(cin >> input)
{
if(find(all_words.begin(), all_words.end(), input) != all_words.end())
{
cout << " it's repeat..…
[/Quote]
hhyttppd 2008-10-29
  • 打赏
  • 举报
回复
		if(all_words.size() > 1 && input.compare(all_words.back()) == 0)
{
cout << " it's repeat... which is " << input << endl;
break;
}
hhyttppd 2008-10-29
  • 打赏
  • 举报
回复

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

typedef vector<string> SIMPLE_WORDS;

int main()
{
SIMPLE_WORDS all_words;

string input;

while(cin >> input)
{
if(find(all_words.begin(), all_words.end(), input) != all_words.end())
{
cout << " it's repeat... which is " << input << endl;
break;
}

all_words.push_back(input);
}

struct{
void operator()(SIMPLE_WORDS::value_type& itr)
{
cout << itr << " ";
}
}print_key;

for_each(all_words.begin(), all_words.end(), print_key);
return 0;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧