经常会遇到格式转换的操作,在C++中可以采用stringstream来进行转换,这种操作还是比较方便的直观的,记录一点使用心得,
1、支持int、char* < --- >string、bool -- > int之间的格式转换;
2、使用stringstream来做转换时,最好使用完,进行ss.clear()操作;
推荐阅读这个链接http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110620.html
使用的一个例子
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41#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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复