第一種情況:輸出正常
#include <isotream>
#include <sstream>
#include <string>
using namespce std;
string intToString(int a)
{
ostringstream ostr;
ostr<<hex<<100;
string str = ostr.str();
cout<<str;
return str;
}
第二種情況:輸出有亂碼
#include <isotream>
#include <sstream>
#include <string>
using namespce std;
string intToString(int a)
{
ostrstream ostr;
ostr<<hex<<100;
string str = ostr.str();
cout<<str;
return str;
}
==========================================================================
ostrstream:是從ostream(輸出資料流類)和strstreambase(字串流基類)派生而來的
istrstream:是從istream(輸入資料流類)和strstreambase(字串流基類)派生而來的
strstream:是從iostream(輸入輸出資料流類)和strstreambase(字串流基類)派生而來的
istrstream類用於執行c風格的串流的輸入操作,也就是一字串數組作為輸入裝置
ostrstream類用於執行c風格的串流的輸入操作,也就是一字串數組作為輸入裝置
strstream類用於執行c風格的串流輸入輸出操作。
ostringstream
istringstream
在c++有兩種字串流,一種是在 sstream中定義,一種是在strstream中定義,他們的實現基本都一樣。
strstream裡包含:
class strstrambuf;
class istrstream
class ostrstream;
class strstream;
他們是基於c字串char *編寫的。
sstream中包含:
class istringstream
class ostringstream
class stringbuf
class stringstream
他們是基於std::string編寫的
因此ostringstream::str()返回的是std::string類型的字串,ostrstream::str()返回的是char *類型的字串
在使用的時候要注意二者的區別。一般情況下推薦使用std::string類型的字串