<sstream>庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進(jìn)行流的輸入、輸出和輸入輸出操作。另外,每個(gè)類都有一個(gè)對(duì)應(yīng)的寬字符集版本。
<sstream>使用string對(duì)象來代替字符數(shù)組。這樣可以避免緩沖區(qū)溢出的危險(xiǎn)。而且,傳入?yún)?shù)和目標(biāo)對(duì)象的類型被自動(dòng)推導(dǎo)出來,即使使用了不正確的格式化符也沒有危險(xiǎn)。
istringstream和ostringstream主要用在內(nèi)核格式化中(用cout的ostream方法將格式化信息寫入string對(duì)象中或是讀取string對(duì)象中的格式化信息)例如:
ostringstream outstr;
double price= 281.00;
char* ps = "for a copy of the ISO/EIC C++ standard!";
outstr << fixed;
outstr << "Pay only$" << price << ps << end;
string msg = outstr.str();
istreamstring允許用istream方法讀取istringsteam對(duì)象中的數(shù)據(jù),也可以用使用string對(duì)象對(duì)istreamsting對(duì)象初始化。簡而言之:istirngstream和ostringstream可以使用
istream和ostream類的方法來管理存儲(chǔ)在字符串的字符數(shù)據(jù)。
stringstream通常是用來做數(shù)據(jù)轉(zhuǎn)換的。相比c庫的轉(zhuǎn)換,它更加安全,自動(dòng)和直接。
例如:#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::stringstream stream;
std::string result;
int i = 1000;
stream << i; //將int輸入流
stream >> result; //從stream中抽取前面插入的int值
std::cout << result << std::endl; // print the string "1000"
}
除了基本類型的轉(zhuǎn)換,也支持char *的轉(zhuǎn)換。
#include <sstream>
#include <iostream>
int main()
{
std::stringstream stream;
char result[8] ;
stream << 8888; //向stream中插入8888
stream >> result; //抽取stream中的值到result
std::cout << result << std::endl; // 屏幕顯示 "8888"
}
需要注意的是,下面的寫法是不正確的:ifsream fs(Filename);
stringsteam buff;
buff << fs.rubf();//這句代碼可以一次性把文件寫入一個(gè)字符串中,然后將Outbuff.str()的值賦給一個(gè)string對(duì)象就可以。
buff << fs;這樣寫是錯(cuò)誤的,看看下面的<<運(yùn)算符的定義就知道了,它不接受這樣的參數(shù)。
但可以這樣寫fs>>buf;這樣寫才正確。
cout << Outbuff << endl;
這樣寫,編譯器可以通過編譯,但是運(yùn)行后是空值。改成這樣的才行:cout << Outbuff.rubf()<< endl;
istringstream和ostringstream在文件流的用法和stringstream的用法類似,必須用rubf方法才可以看到內(nèi)容。
rubf返回的一個(gè)stringbuf 對(duì)象的指針,str方法返回的是一個(gè)string對(duì)象,上面的rubf也可以換成str方法。
這三個(gè)類的str和rubf的類方法用法都一樣。
不同的是str方法:有兩個(gè)版本:
string str()const;//拷貝流緩沖到一個(gè)string對(duì)象中
void str(constr string& s);//通過流緩沖構(gòu)造一個(gè)string對(duì)象。上面的rubf也可以寫出Outbuff.rubuf()->str(),這樣些效率更高些。
需要特別注意的是:要清空上面的類對(duì)象的內(nèi)存,不能用clear方法,那只是設(shè)置了錯(cuò)誤標(biāo)志位,要用str("");
stringstream的<<方法和ostream的 <<方法一樣。
而且stringstream只有<<運(yùn)算符可以用。
ostream& operator<< (bool& val );
ostream& operator<< (short& val );
ostream& operator<< (unsigned short& val );
ostream& operator<< (int& val );
ostream& operator<< (unsigned int& val );
ostream& operator<< (long& val );
ostream& operator<< (unsigned long& val );
ostream& operator<< (float& val );
ostream& operator<< (double& val );
ostream& operator<< (long double& val );
ostream& operator<< (void*& val );
ostream& operator<< (streambuf* sb );
ostream& operator<< (ostream& ( *pf )(ostream&));
ostream& operator<< (ios& ( *pf )(ios&));
ostream& operator<< (ios_base& ( *pf )(ios_base&));
上面的都是它的成員函數(shù),下面的則是全局函數(shù)
ostream& operator<< (ostream& out, char c );
ostream& operator<< (ostream& out, signed char c );
ostream& operator<< (ostream& out, unsigned char c );
ostream& operator<< (ostream& out, const char* s );
ostream& operator<< (ostream& out, const signed char* s );
ostream& operator<< (ostream& out, const unsigned char* s );
我們還可以利用stringstream來清楚文件內(nèi)容。示例代碼如下:
ofstream fs(FileName);
stringstream str;
str<<fs;
fs.close();
這樣文件就被清空了,但是文件還在。