总结了 streamstream 用法
stringstream用法
参考链接 https://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html
需要在源程序文件中包含头文件include<sstream>
包含的类 有 stringstream、istringstream、ostringstream
数组转字符串
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
| #include <iostream> #include <sstream> using namespace std; int main(void) { double pi = 3.141592653589793; float dollar = 1.00; int dozen = 12; int number = 35; stringstream ss; ss << "dozen: " << dozen << endl; ss.setf(ios::fixed); ss.precision(2); ss << "dollar: " << dollar << endl; ss.precision(10); ss << "pi: " << pi << endl; ss.unsetf(ios_base::dec); ss.setf(ios::hex); ss << "number: " << number << endl; string text = ss.str(); cout << text << endl; return 0; }
|
这个示例的本质是:数字 -> stringstream对象 -> string
字符串->数字
string -> double/int
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
| #include <iostream> #include <sstream> using namespace std; int main() { double dVal; int iVal; string str; stringstream ss; str = "123.456789"; ss << str; ss >> dVal; cout << "dVal: " << dVal << endl; str = "654321"; ss.clear(); ss << str; ss >> iVal; cout << "iVal: " << iVal << endl; return 0; }
|
这个示例的本质是:string -> stringstream对象 -> 数字