試利用 stringstream 型別,將多個字串串起來,
再利用 str() 函式將物件中的字串帶出來。
![](https://seed.istak.org.tw/attachment.php?aid=ODg0Nnw3NmE3NGQ4OHwxNTkwNzM1NDI1fDUwY2FFZzdBZ3d1UklENGhUMUN4V0VJTXlEM1BmVThqbURITUJ3bFpzck1ZakNr&noupdate=yes) -
- #include<iostream>
- #include<cstdlib>
- #include<sstream>
- using namespace std;
- int main()
- {
- stringstream ss;
- string str1="abc";
- string str2="狗咬豬";
- int a=12;
- float b=3.456;
- ss<<str1<<str2<<a<<b<<"...";
- cout<<ss.str()<<endl;
- system("pause");
- return 0;
- }
複製代碼- #include<iostream>
- #include<cstdlib>
- #include<sstream>
- using namespace std;
- int main()
- {
- stringstream ss;
- string str1="abc";
- string str2="狗咬豬";
- int a=12;
- float b=3.456;
- ss<<str1<<str2<<a<<b<<"...";
- string str;
- ss>>str;
- cout<<str<<endl;
- system("pause");
- return 0;
- }
複製代碼 |