int转成string push_back到vector中

zht_304 2009-11-18 02:12:57
#include <list>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

int main()
{
stringstream sout;
string s;
vector<string> svec;
for(int i = 0; i < 10; i++){
//sout.flush();
sout << i; // 这样可以嘛?
s = sout.str(); // ??
svec.push_back(s);
}
list<string> slist(svec.begin(), svec.end());

for(list<string>::iterator iter = slist.begin();
iter != slist.end();
++ iter){
//cout.flush();
cout << *iter << " ";

}
cout << endl;
system("pause");
return 0;
}


输出是:
0 01 012 0123 01234 012345 0123456 01234567 012345678 0123456789
我想做到的是:
0 1 2 3 4 5.。。。9
...全文
95 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zht_304 2009-11-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 loaden 的回复:]
这样就行了:
加了一个sout.str("");
C/C++ code#include<list>
#include<vector>
#include<string>
#include<sstream>
#include<iostream>usingnamespace std;int main()
{
stringstream sout;string s;
vector<string>svec;for (int i=0; i<10; i++)
{//sout.flush(); sout<< i;// 这样可以嘛? s= sout.str();// ?? svec.push_back(s);
sout.str("");
}
list<string>slist(svec.begin(), svec.end());for (list<string>::iterator iter= slist.begin(); iter!= slist.end();++iter)
{//cout.flush(); cout<<*iter<<"";

}
cout<< endl;
system("pause");return0;
}
[/Quote]

谢谢。 这样可以。
zht_304 2009-11-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 loaden 的回复:]
少了一个:sout.clear();
[/Quote]
为什么不行啊?
老邓 2009-11-18
  • 打赏
  • 举报
回复
输出:

Microsoft (R) Program Maintenance Utility Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.

0 1 2 3 4 5 6 7 8 9
请按任意键继续. . .
老邓 2009-11-18
  • 打赏
  • 举报
回复
这样就行了:
加了一个sout.str("");
#include <list>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

int main()
{
stringstream sout;
string s;
vector<string>svec;
for (int i = 0; i < 10; i++)
{
//sout.flush();
sout << i; // 这样可以嘛?
s = sout.str(); // ??
svec.push_back(s);
sout.str("");
}
list<string>slist(svec.begin(), svec.end());

for (list<string>::iterator iter = slist.begin(); iter != slist.end(); ++iter)
{
//cout.flush();
cout << *iter << " ";

}
cout << endl;
system("pause");
return 0;
}
Jinhao 2009-11-18
  • 打赏
  • 举报
回复

#include <list>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

int main()
{
stringstream sout;
string s; //这个s是多余的
vector<string> svec;
for(int i = 0; i < 10; i++){
//sout.flush();
sout.str(""); //加上这行
sout << i; // 这样可以嘛?
//s = sout.str(); // 去掉
svec.push_back(sout.str());
}
list<string> slist(svec.begin(), svec.end());

for(list<string>::iterator iter = slist.begin();
iter != slist.end();
++ iter){
//cout.flush();
cout << *iter << " ";

}
cout << endl;
system("pause");
return 0;
}
老邓 2009-11-18
  • 打赏
  • 举报
回复
少了一个:sout.clear();

64,654

社区成员

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

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