返回列表 發帖

stringstream 字串串流 (一)

試利用 stringstream 型別,將多個字串串起來,
再利用 str() 函式將物件中的字串帶出來。


  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     string str1="abc";
  9.     string str2="狗咬豬";
  10.     int a=12;
  11.     float b=3.456;
  12.     ss<<str1<<str2<<a<<b<<"...";
  13.     cout<<ss.str()<<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 str1="abc";
  9.     string str2="狗咬豬";
  10.     int a=12;
  11.     float b=3.456;
  12.     ss<<str1<<str2<<a<<b<<"...";
  13.     string str;
  14.     ss>>str;
  15.     cout<<str<<endl;
  16.     system("pause");
  17.     return 0;
  18. }
複製代碼

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. stringstream ss;
  4. int main()
  5. {
  6.     cin.sync_with_stdio(0);
  7.     cin.tie();
  8.     string str;
  9.     string str1="abc";
  10.     string str2="狗咬豬";
  11.     int a=12;
  12.     float b=3.456;
  13.     ss<<str1<<str2<<a<<b;
  14.     cout<<ss.str()<<endl;
  15.     ss>>str;
  16.     cout<<str;
  17.     return 0;
  18. }
複製代碼
hahahahahahahaha

TOP

  1. #include<bits/stdc++.h>
  2. #include<sstream>
  3. using namespace std;
  4. int main()
  5. {
  6.     stringstream ss;
  7.     string str1="abc";
  8.     string str2="狗咬豬";
  9.     int a=12;
  10.     float b=3.456;
  11.     ss<<str1<<str2<<a<<b<<"...";
  12.     cout<<ss.str()<<endl;
  13.     return 0;   
  14. }
複製代碼
Ivy

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. using namespace std;
  5. int main()
  6. {
  7.     stringstream ss;
  8.     string str1="abc";
  9.     string str2="狗咬豬";
  10.     int a=12;
  11.     float b=3.456;
  12.     ss<<str1<<str2<<a<<b<<"...";
  13.     string str;
  14.     ss>>str;
  15.     cout<<str<<endl;
  16.     system("pause");
  17.     return 0;
  18. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define int ll
  5. #define FOR(i,a,b) for(int i=a;i<b;i++)
  6. #define REP(i,n) FOR(i,0,n)
  7. #define REP1(i,n) FOR(i,1,(n)+1)
  8. #define RREP(i,n) for(int i=(n)-1;i>=0;i--)
  9. #define f first
  10. #define s second
  11. #define pb push_back
  12. #define ALL(x) x.begin(),x.end()
  13. #define SZ(x) (int)(x.size())
  14. #define SQ(x) (x)*(x)
  15. #define pii pair<int,int>
  16. #define Graph vector<vector<int>>
  17. #define IOS() cin.sync_with_stdio(0),cin.tie(0),cout.tie(0)
  18. const ll inf=(1ll<<63)-1;
  19. const int maxn=1e4+5;
  20. const ll mod=1e9+7;
  21. stringstream ss;
  22. string str1="abc";
  23. string str2="狗咬豬";
  24. int a=12;
  25. float b=3.456;
  26. signed main()
  27. {
  28.     IOS();
  29.     ss<<str1<<str2<<a<<b<<"...";
  30.     cout<<ss.str()<<"\n";
  31.     return 0;
  32. }
複製代碼
Allen

TOP

返回列表