概述
经常会遇到格式转换的操作,在C++中可以采用stringstream来进行转换,这种操作还是比较方便的直观的,记录一点使用心得,
1、支持int、char* < --- >string、bool -- > int之间的格式转换;
2、使用stringstream来做转换时,最好使用完,进行ss.clear()操作;
推荐阅读这个链接http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110620.html
使用的一个例子
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
vector<int> v;
string tmp;
while(getline(cin,tmp))
{
//int itmp = atoi(tmp.c_str());
int itmp;
stringstream ss;
ss << tmp;
ss >> itmp;
ss.clear();
/*
if(!ss.good())
{
cerr << "bad convertn";
break;
}
*/
v.push_back(itmp);
cin.sync();
}
sort(v.begin(),v.end());
/*
vector<int>::iterator it = v.begin();
for(;it != v.end();it++)
{
cout << *it << endl;
}
*/
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"n"));
//cout << "Hello world!" << endl;
return 0;
}
最后
以上就是多情哈密瓜为你收集整理的C++之sstream的全部内容,希望文章能够帮你解决C++之sstream所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复