將C++數(shù)字類(lèi)型轉(zhuǎn)換成字符串
#include#include#includeusing?namespace?std;
int?main()
{
?int?a?=?55;
?double?b?=?65.123;
?string?str?=?"";
?//頭文件是sstream
?ostringstream?oss;
?oss?<<?a?<<?"---"?<<?b;
?str?=?oss.str();
?cout?<<?str?<<?endl;
?return?0;
}實(shí)現(xiàn)這個(gè)目標(biāo),非stringstream類(lèi)莫屬。
這個(gè)類(lèi)在頭文件中定義, < sstream>庫(kù)定義了三種類(lèi):istringstream、ostringstream和stringstream,分別用來(lái)進(jìn)行流的輸入、輸出和輸入輸出操作。另外,每個(gè)類(lèi)都有一個(gè)對(duì)應(yīng)的寬字符集版本。
簡(jiǎn)單起見(jiàn),我主要以stringstream為中心,因?yàn)槊總€(gè)轉(zhuǎn)換都要涉及到輸入和輸出操作。
示例1示范怎樣使用一個(gè)stringstream對(duì)象進(jìn)行從 string到int類(lèi)型的轉(zhuǎn)換 注意,使用string對(duì)象來(lái)代替字符數(shù)組。這樣可以避免緩沖區(qū)溢出的危險(xiǎn)。而且,傳入?yún)?shù)和目標(biāo)對(duì)象的類(lèi)型被自動(dòng)推導(dǎo)出來(lái),即使使用了不正確的格式化符也沒(méi)有危險(xiǎn)。
示例1:
復(fù)制代碼 代碼如下:
std::stringstream stream;
string result="10000";
int n = 0;
stream << result; stream >> n;//n等于10000
int到string類(lèi)型的轉(zhuǎn)換
復(fù)制代碼 代碼如下:
string result;
int n = 12345;
stream << n;
result =stream.str();// result等于"12345"
重復(fù)利用stringstream對(duì)象 如果你打算在多次轉(zhuǎn)換中使用同一個(gè)stringstream對(duì)象,記住再每次轉(zhuǎn)換前要使用clear()方法,在多次轉(zhuǎn)換中重復(fù)使用同一個(gè) stringstream(而不是每次都創(chuàng)建一個(gè)新的對(duì)象)對(duì)象最大的好處在于效率。stringstream對(duì)象的構(gòu)造和析構(gòu)函數(shù)通常是非常耗費(fèi)CPU 時(shí)間的。經(jīng)試驗(yàn),單單使用clear()并不能清除stringstream對(duì)象的內(nèi)容,僅僅是了該對(duì)象的狀態(tài),要重復(fù)使用同一個(gè) stringstream對(duì)象,需要使用str()重新初始化該對(duì)象。
示例2:
復(fù)制代碼 代碼如下:
std::stringstream strsql;
for (int i= 1; i < 10; ++i)
{
strsql << "insert into test_tab values(";
strsql << i << ","<< (i+10) << ");";
std::string str = strsql.str();// 得到string
res = sqlite3_exec(pDB,str.c_str(),0,0, &errMsg);
std::cout << strsql.str() << std::endl; strsql.clear();
strsql.str("");
}
轉(zhuǎn)換中使用模板 也可以輕松地定義函數(shù)模板來(lái)將一個(gè)任意的類(lèi)型轉(zhuǎn)換到特定的目標(biāo)類(lèi)型。
例如,需要將各種數(shù)字值,如int、long、double等等轉(zhuǎn)換成字符串,要使用以一個(gè)string類(lèi)型和一個(gè)任意值t為參數(shù)的to_string()函數(shù)。
to_string()函數(shù)將t轉(zhuǎn)換為字符串并寫(xiě)入result中。
使用str()成員函數(shù)來(lái)獲取流內(nèi)部緩沖的一份拷貝:
示例3:
復(fù)制代碼 代碼如下:
template void to_string(string & result,const T& t)
{ ostringstream oss;//創(chuàng)建一個(gè)流 oss< out_type convert(const in_value & t)
{ stringstream stream; stream<>result;//向result中寫(xiě)入值 return result; }
這樣使用convert(): double d; string salary; string s=”12.56”; d=convert(s);//d等于12.56 salary=convert(9000.0);//salary等于”9000”
結(jié)論:在過(guò)去留下來(lái)的程序代碼和純粹的C程序中,傳統(tǒng)的形式的轉(zhuǎn)換伴隨了我們很長(zhǎng)的一段時(shí)間。但是,如文中所述,基于 stringstream的轉(zhuǎn)換擁有類(lèi)型安全和不會(huì)溢出這樣搶眼的特性,使我們有充足得理由拋棄而使用< sstream>。
當(dāng)然現(xiàn)在還有一個(gè)更好的選擇,那就是使用boost庫(kù)中的lexical_cast,它是類(lèi)型安全的轉(zhuǎn)換。
如下例:
復(fù)制代碼 代碼如下:
#include #include #include #include #include
?using namespace std;
using namespace boost;
int main(void)
try
{
//以下是內(nèi)置類(lèi)型向string轉(zhuǎn)換的解決方案
//lexical_cast優(yōu)勢(shì)明顯
int ival;
char cval;
ostringstream out_string;
string str0;
string str1;
ival = 100;
cval = 'w';
out_string << ival << " " << cval;
str0 = out_string.str();
str1 = lexical_cast(ival) + lexical_cast(cval);
cout << str0 << endl; cout << str1 << endl;
//以下是string向內(nèi)置類(lèi)型轉(zhuǎn)換的解決方案
//幾乎和stringstrem相比,lexical_cast就是類(lèi)型安全的,
int itmpe;
char ctmpe;
str0 = "100k";
str1 = "100h";
istringstream in_string( str0 );
in_string >> itmpe >> ctmpe;
cout << itmpe << " " << ctmpe << endl;
itmpe = lexical_cast(str1);
ctmpe = lexical_cast(str1);
system( "PAUSE" );
return 0;
} catch(bad_lexical_cast e)
{ cout << e.what() << endl; cin.get(); }





