試利用 stringstream 型別, 來進行 int 與 string 的相互轉換.- #include<iostream>
- #include<sstream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- stringstream ss;
- int a=123;
- string str;
- cout<<"整數型態: "<<a<<endl;
- ss<<a;
- ss>>str;
- cout<<"字串型態: "<<str<<endl;
- system("pause");
- return 0;
- }
複製代碼- #include<iostream>
- #include<sstream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- stringstream ss;
- int a=123;
- string str;
- cout<<"整數型態: "<<a+456<<endl;
- ss<<a;
- ss>>str;
- cout<<"字串型態: "<<str+"456"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |