帮看看这段代码吧

XiaoG602 2008-06-23 07:28:22
//为什么运行的时候会出错啊?谢谢
[code=C/C++][#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;

int main()
{
list<char *> clist;
vector<string> svec;
char *temp;
while(cin>>temp)
clist.push_back(temp);
svec.assign(clist.begin(),clist.end());
for(vector<string>::size_type ix=0;ix!=svec.size();++ix)
cout<<svec[ix]<<' ';
cout<<endl;
system("pause");
return 0;
} /code]
...全文
60 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttkk_2007 2008-06-23
  • 打赏
  • 举报
回复

//需要这么麻烦吗,直接
int main()
{
list <char *> clist;
vector <string> svec;
char *temp = new char[100];
while(cin>>temp)
svec.push_back(temp);
//svec.assign(clist.begin(),clist.end());
for(vector <string>::size_type ix=0;ix!=svec.size();++ix)
cout <<svec[ix] <<' ';
cout <<endl;
delete []temp;
system("pause");
return 0;
}
K行天下 2008-06-23
  • 打赏
  • 举报
回复

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

int main()
{
list <char *> clist;
vector <string> svec;
char *temp = new char[100];
while(cin>>temp)
{
clist.push_back(temp);
//再说几句,如果像上面那样,下面的输出用于只是最后一次输入的数据
// 因为你只分配了一次内存空间, 后面的输入覆盖了前面的数据,因此,输入完一组数据后,要再一次
// 分配内存空间!!!!!
temp = new char[100];
}
svec.assign(clist.begin(),clist.end());
for(vector <string>::size_type ix=0;ix!=svec.size();++ix)
cout <<svec[ix] <<' ';
cout <<endl;
system("pause");
return 0;
}

K行天下 2008-06-23
  • 打赏
  • 举报
回复

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

int main()
{
list <char *> clist;
vector <string> svec;
char *temp = new char[100];
while(cin>>temp)
clist.push_back(temp);
svec.assign(clist.begin(),clist.end());
for(vector <string>::size_type ix=0;ix!=svec.size();++ix)
cout <<svec[ix] <<' ';
cout <<endl;
system("pause");
return 0;
}


pengzhixi 2008-06-23
  • 打赏
  • 举报
回复
用string 多好,用什么char *啊
bitxinhai 2008-06-23
  • 打赏
  • 举报
回复
char *temp;
while(cin>>temp)

这两句你觉得对吗???

64,687

社区成员

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

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