返回列表 發帖

stringstream 字串串流 (二)

本帖最後由 鄭繼威 於 2023-3-22 20:31 編輯

試利用 stringstream 型別,來進行變數型態的轉換,譬如 int 轉 float、double轉 string 等。
stringstream 物件再重複使用前,必須先做初始化 (清空) 的動作。
我們可透過 <typeinfo> 標頭檔所提供的 typeid() 接 name() 函式,查看變數的型態。
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. #include<typeinfo>
  5. using namespace std;
  6. int main()
  7. {
  8.     stringstream ss;
  9.     int a=123;
  10.     double b=456.789;
  11.     float c;
  12.     string d;
  13.    
  14.     ss<<a;
  15.     ss>>c;
  16.     cout<<"a="<<a<<",型態為:"<<typeid(a).name()<<endl;
  17.     cout<<"c="<<c<<",型態為:"<<typeid(c).name()<<endl;
  18.    
  19.     //重複使用前需初始化
  20.     ss.clear();
  21.    
  22.     ss<<b;
  23.     ss>>d;
  24.     cout<<"b="<<b<<",型態為:"<<typeid(b).name()<<endl;
  25.     cout<<"d="<<d<<",型態為:"<<typeid(d).name()<<endl;
  26.    
  27.    
  28.     system("pause");
  29.     return 0;
  30. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream s;
  8.     int a=123;
  9.     double b=456.789;
  10.     float c;
  11.     string d;
  12.     s<<a;
  13.     s>>c;
  14.     cout<<"a="<<a<<",型態為:"<<typeid(a).name()<<endl;
  15.     cout<<"c="<<c<<",型態為:"<<typeid(c).name()<<endl;
  16.     s.clear();
  17.     s<<b;
  18.     s>>d;
  19.     cout<<"b="<<b<<",型態為:"<<typeid(b).name()<<endl;
  20.     cout<<"d="<<d<<",型態為:"<<typeid(d).name()<<endl;
  21.     system("pause");
  22.     return 0;
  23. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. #include<typeinfo>
  5. using namespace std;
  6. int main()
  7. {
  8.     stringstream ss;
  9.     int a=123;
  10.     double b=456.789;
  11.     float c;
  12.     string d;
  13.    
  14.     ss<<a;
  15.     ss>>c;
  16.     cout<<"a="<<a<<",型態為:"<<typeid(a).name()<<endl;
  17.     cout<<"c="<<c<<",型態為:"<<typeid(c).name()<<endl;
  18.     ss.clear();
  19.    
  20.     ss<<b;
  21.     ss>>d;
  22.     cout<<"b="<<b<<",型態為:"<<typeid(b).name()<<endl;
  23.     cout<<"d="<<d<<",型態為:"<<typeid(d).name()<<endl;
  24.    
  25.    
  26.     system("pause");
  27.     return 0;
  28. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. #include<typeinfo>
  5. using namespace std;
  6. int main()
  7. {
  8.     stringstream ss;
  9.     int a=123;
  10.     double b=456.789;
  11.     float c;
  12.     string d;
  13.     ss<<a;
  14.     ss>>c;
  15.     cout<<"a="<<a<<",型態為:"<<typeid(a).name()<<endl;
  16.     cout<<"c="<<c<<",型態為:"<<typeid(c).name()<<endl;
  17.     ss.clear();
  18.     ss<<b;
  19.     ss>>d;
  20.     cout<<"b="<<b<<",型態為:"<<typeid(b).name()<<endl;
  21.     cout<<"d="<<d<<",型態為:"<<typeid(d).name()<<endl;
  22.     system("pause");
  23.     return 0;
  24. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream s;
  8.     int a=123;
  9.     double b=456.789;
  10.     float c;
  11.     string d;
  12.     s<<a;
  13.     s>>c;
  14.     cout<<"a="<<a<<",型態為:"<<typeid(a).name()<<endl;
  15.     cout<<"c="<<c<<",型態為:"<<typeid(c).name()<<endl;
  16.     s.clear();
  17.     s<<b;
  18.     s>>d;
  19.     cout<<"b="<<b<<",型態為:"<<typeid(b).name()<<endl;
  20.     cout<<"d="<<d<<",型態為:"<<typeid(d).name()<<endl;
  21.     system("pause");
  22.     return 0;
  23. }
複製代碼

TOP

返回列表