习题求教

opposever 2008-05-01 09:11:17
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#pragma warning(disable:4786)
int main()
{
string s1;
vector<string> ivec;
vector<string>::iterator beg=ivec.begin(),end=ivec.end();

while(cin>>s1)
{
ivec.push_back(s1);
}
for(vector<string>::iterator iter=beg;iter!=end;++iter)
cout<<*iter<<endl;
return 0;
}
C++ pirmer习题9.14 读入string对象,存储在vector对象中,然后输出该vector对象中的所有元素。
这里输入后按ctrl+z没反映,再按一下就退出了,看不到输出结果,是哪里有问题啊?
...全文
88 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
godspeed_wxw 2008-05-02
  • 打赏
  • 举报
回复
在for循环条件中调用 end()
能够根据向量中元素的变化及时的刷新 末位的哨兵
xian_hf 2008-05-01
  • 打赏
  • 举报
回复
1楼正解,路过学习
tjj5203 2008-05-01
  • 打赏
  • 举报
回复
你定义的end=ivec.end();是在插入之前的,这时begin()跟end()指向同一个 位子!
插入后可能就失效了!因为插入后的end可能就不是你定义的位子了!
tjj5203 2008-05-01
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#pragma warning(disable:4786)
int main()
{
string s1;
vector <string> ivec;
while(cin>>s1)
{
ivec.push_back(s1);
}
vector <string>::iterator beg=ivec.begin();//这句这样!
for(vector <string>::iterator iter=beg;iter!=ivec.end();++iter)
cout <<*iter <<endl;
system("pause");
return 0;
}
jy01807853 2008-05-01
  • 打赏
  • 举报
回复
不要把END返回的直储存在局部变量里面
谍代器失效
hujinyong199 2008-05-01
  • 打赏
  • 举报
回复
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
string s1;
vector <string> ivec;
//vector <string>::iterator beg=ivec.begin(),end=ivec.end(); //迭代器失效!

while(cin>>s1)
{
ivec.push_back(s1);
}
for(vector <string>::iterator iter= ivec.begin();iter!=ivec.end();++iter)
cout <<*iter <<endl;
return 0;
}



UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
什么呀

64,637

社区成员

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

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