push_back问题??

haolly 2011-09-23 01:52:13

#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
int main()
{
string str;
vector<string> strn;
while(cin>>str)
strn.push_back(str);
cout<<str;
return 0;
}

输入andy 按enter再输入haolly,然后按enter,ctrl+z,ENTER
为什么只输出haolly??
...全文
94 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
haolly 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fox000002 的回复:]

C/C++ code
cout<<str // 只是输出 str 这个字符串,还想是什么
[/Quote]


#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
int main()
{
string str;
vector<string> strn;
while(cin>>str)
strn.push_back(str);
cout<<strn;
return 0;
}

其实是这样的,弄错了
gfy5216 2011-09-23
  • 打赏
  • 举报
回复
同学啊,你cout的是str,而str最后输入赋值的是最后面的haolly。你是不是想输出容器里面的内容啊。如果只是想输出str,你费这么些劲干嘛啊
luckygxboy 2011-09-23
  • 打赏
  • 举报
回复
lz觉得应该输出什么?
luciferisnotsatan 2011-09-23
  • 打赏
  • 举报
回复
copy(strn.begin(),strn.end(),ostream_iterator<string>(cout, "\n"));
luciferisnotsatan 2011-09-23
  • 打赏
  • 举报
回复
cout<<str; 当然只输出str里的内容
fbling11 2011-09-23
  • 打赏
  • 举报
回复
空白字符指空格、水平制表、垂直制表、换页、回车和换行符
fbling11 2011-09-23
  • 打赏
  • 举报
回复
while(cin>>str)
strn.push_back(str);
cout<<str;

1、cout<<str;不在while 循环内,只保留了最新输入的值。
2、string 类型的输入操作符(>>)读取并省略所有的开头和结尾的空白字符,结尾处遇到空白字符则停止。
fox000002 2011-09-23
  • 打赏
  • 举报
回复
cout<<str // 只是输出 str 这个字符串,还想是什么
bdmh 2011-09-23
  • 打赏
  • 举报
回复
ctrl+z,退出

33,319

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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