问个vector的简单问题,帮帮忙啊

zhangyiviva 2008-11-05 06:03:53
string word;
vector<string> text;
while (cin>>word){
text.push_back(word);
}
这个小程序是不是有问题???
比如我输入“asd”
然后cout<<text[1];
怎么不能输出?????
请指教啊
...全文
85 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangyiviva 2008-11-05
  • 打赏
  • 举报
回复
可是我输入字符串后按了回车了啊???
这样也不能输出???
机智的呆呆 2008-11-05
  • 打赏
  • 举报
回复

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

int main()
{
string word;
vector <string> text;
while (cin>>word)
{
text.push_back(word);

}
for(size_t i=0;i<text.size();i++)
cout<<text[i]<<endl;

system("pause");
}


ctrl+z 结束输入
OenAuth.Net 2008-11-05
  • 打赏
  • 举报
回复


#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string word;
vector <string> text;
while (cin>>word&&word!="q")//第一个输入asd 第二行输入q,我在这里用q退出循环,不然你的程序一直是输入,到不了下面。
{
text.push_back(word);
}
cout<<text[0]<<endl;
return 0;
}
//或者这样
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string word;
vector <string> text;
while (cin>>word)
{
text.push_back(word);
cout<<text[0]<<endl;
}
return 0;
}
frisky_lobo 2008-11-05
  • 打赏
  • 举报
回复
	string word; 
vector <string> text;
int i = 0;
while (i<5){
cin>>word;
text.push_back(word);
cout << text[i++]<<endl;
}

因为你没有刷新,endl
final_authority 2008-11-05
  • 打赏
  • 举报
回复
楼主把代码全贴上来吧
zhangyiviva 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 stardust20 的回复:]
"asd"是存在text[0]中的
[/Quote]可是为什么text[0]也不能输出呢?
frisky_lobo 2008-11-05
  • 打赏
  • 举报
回复
	string word; 
vector <string> text;
int i = 2;
while (i--){
cin>>word;
text.push_back(word);
cout << word;
}
pusshi 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 stardust20 的回复:]
"asd"是存在text[0]中的
[/Quote]
C++中所以容器下标都是从0开始的,
pusshi 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 stardust20 的回复:]
"asd"是存在text[0]中的
[/Quote]
所有的数组类似的容器,下标都是从0开始的
stardust20 2008-11-05
  • 打赏
  • 举报
回复
"asd"是存在text[0]中的

65,211

社区成员

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

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