返回列表 發帖

int 與 string 的相互轉換

試利用 stringstream 型別, 來進行 int 與 string 的相互轉換.
  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=123;
  9.     string str;
  10.     cout<<"整數型態: "<<a<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼
  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=123;
  9.     string str;
  10.     cout<<"整數型態: "<<a+456<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str+"456"<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼

TOP

TOP

本帖最後由 戴安利 於 2019-5-10 20:20 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=123;
  9.     string str;
  10.     cout<<"整數型態: "<<a<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str<<endl;
  14.       
  15.    system("pause");
  16.    return 0;
  17. }
複製代碼
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=123;
  9.     string str;
  10.     cout<<"整數型態: "<<a<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str+"456"<<endl;
  14.       
  15.    system("pause");
  16.    return 0;
  17. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=123;
  9.     string str;
  10.     cout<<"整數型態: "<<a<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼
  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=123;
  9.     string str;
  10.     cout<<"整數型態: "<<a+456<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str+"456"<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=5678;
  9.     string str;
  10.     cout<<"整數型態: "<<a<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼
  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=45678;
  9.     string str;
  10.     cout<<"整數型態: "<<a+90<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str+"90"<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=123;
  9.     string str;
  10.     cout<<"數字"<<a<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串"<<str<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼
  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=123;
  9.     string str;
  10.     cout<<"整數型態"<<a+456<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態"<<str+"456"<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     string n="123";
  9.     int x;
  10.     ss<<n;
  11.     ss>>x;
  12.     cout<<"整數型態:"<<x<<endl;
  13.     cout<<"字串型態:"<<n<<endl;
  14.     system("pause");
  15.     return 0;
  16. }
複製代碼
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     string n;
  9.     int x=123;
  10.     ss<<x;
  11.     ss>>n;
  12.     cout<<"整數型態:"<<x<<endl;
  13.     cout<<"字串型態:"<<n<<endl;
  14.     system("pause");
  15.     return 0;
  16. }
複製代碼

TOP

本帖最後由 蔡依宸 於 2019-5-4 16:49 編輯
  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=123;
  9.     string str;
  10.     cout<<"整數型態: "<<a<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼
  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int a=123;
  9.     string str;
  10.     cout<<"整數型態: "<<a+456<<endl;
  11.     ss<<a;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str+"456"<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼

TOP

本帖最後由 蔡季庭 於 2019-5-4 16:48 編輯
  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     int n=123;
  9.     string str;
  10.     cout<<"整數型態: "<<n+1<<endl;
  11.     ss<<n;
  12.     ss>>str;
  13.     cout<<"字串型態: "<<str<<endl;
  14.     system("pause");     
  15.     return 0;   
  16. }
複製代碼
#include<iostream>
  1. #include<sstream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     stringstream ss;
  7.     int n=123;
  8.     string str;
  9.     cout<<"整數型態: "<<n+1<<endl;
  10.     ss<<n;
  11.     ss>>str;
  12.     cout<<"字串型態: "<<str<<endl;
  13.     system("pause");     
  14.     return 0;   
  15. }
複製代碼

TOP

返回列表