c++ string與int通過流相互轉化
阿新 • • 發佈:2019-01-09
stringstream :字串流 , 需要包含標頭檔案#include<sstream>
stringstream使用操作符<< 、 >>,流在操作符的左邊
>>:從流中輸出東西
<<:輸入東西到流中
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { /*字串轉化為數字*/ string s; cin >> s; //從流(cin)中輸出東西到字串s中 stringstream st; //字串流 st << s; //輸入s字串到流(st)中 int n; st >> n; //從流(st)中輸出東西到n; cout << n << endl; //輸入n到流(cout)中 /*數字轉化為字串*/ int m = 12345; stringstream st2; st2 << m; string s2; st2 >> s2; cout << s2 << endl; system("pause"); return 0; }
執行結果: