Posted on 2009-02-13 03:16
藍塵 閱讀(384)
評論(0) 編輯 收藏 引用 所屬分類:
轉載
用標準c++進行string與各種內置數據類型的轉換
要實現這個目標,非 stringstream 類莫屬。這個類在<sstream>頭文件中定義, <sstream>庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進行流的輸入、輸出和輸入輸出操作。另外,每個類都有一個對應的寬字符集版本。簡單起見,我主要以stringstream為中心,因為每個轉換都要涉及到輸入和輸出操作。示例1示范怎樣使用一個stringstream對象進行從
string到int類型的轉換
注意,<sstream>使用string對象來代替字符數組。這樣可以避免緩沖區溢出的危險。而且,傳入參數和目標對象的類型被自動推導出來,即使使用了不正確的格式化符也沒有危險。
示例1:
std::stringstream stream;
string result="10000";
int n = 0;
stream << result;
stream >> n;//n等于10000
int到string類型的轉換
string result;
int n = 12345;
stream << n;
result =stream.str();// result等于"12345"
重復利用stringstream對象
如果你打算在多次轉換中使用同一個stringstream對象,記住再每次轉換前要使用clear()方法,在多次轉換中重復使用同一個stringstream(而不是每次都創建一個新的對象)對象最大的好處在于效率。stringstream對象的構造和析構函數通常是非常耗費CPU時間的。經試驗,單單使用clear()并不能清除stringstream對象的內容,僅僅是了該對象的狀態,要重復使用同一個stringstream對象,需要使用str()重新初始化該對象。
示例2:
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("");
}
轉換中使用模板
也可以輕松地定義函數模板來將一個任意的類型轉換到特定的目標類型。例如,需要將各種數字值,如int、long、double等等轉換成字符串,要使用以一個string類型和一個任意值 t 為參數的to_string()函數。to_string()函數將 t 轉換為字符串并寫入result中。使用str()成員函數來獲取流內部緩沖的一份拷貝:
示例3:
template<class T>
void to_string(string & result,const T& t)
{
ostringstream oss;//創建一個流
oss<<t;//把值傳遞如流中
result=oss.str();//獲取轉換后的字符轉并將其寫入result
}
這樣,你就和衣輕松地將多種數值轉換成字符串了:
to_string(s1,10.5);//double到string
to_string(s2,123);//int到string
to_string(s3,true);//bool到string
可以更進一步定義一個通用的轉換模板,用于任意類型之間的轉換。函數模板convert()含有兩個模板參數out_type和in_value,功能是將in_value值轉換成out_type類型:
template<class out_type,class in_value>
out_type convert(const in_value & t)
{
stringstream stream;
stream<<t;//向流中傳值
out_type result;//這里存儲轉換結果
stream>>result;//向result中寫入值
return result;
}
這樣使用convert():
double d;
string salary;
string s=”12.56”;
d=convert<double>(s);//d等于12.56
salary=convert<string>(9000.0);//salary等于”9000”
結論
在過去留下來的程序代碼和純粹的C程序中,傳統的<stdio.h>形式的轉換伴隨了我們很長的一段時間。但是,如文中所述,基于stringstream的轉換擁有類型安全和不會溢出這樣搶眼的特性,使我們有充足得理由拋棄<stdio.h>而使用<sstream>。
當然現在還有一個更好的選擇,那就是使用boost庫中的lexical_cast,它是類型安全的轉換。如下例:
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
int main(void)
try{
//以下是內置類型向string轉換的解決方案
//lexical_cast優勢明顯
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<string>(ival)
+ lexical_cast<string>(cval);
cout << str0 << endl;
cout << str1 << endl;
//以下是string向內置類型轉換的解決方案
//幾乎和stringstrem相比,lexical_cast就是類型安全的,
int itmpe;
char ctmpe;
str0 = "100k";
str1 = "100h";
istringstream in_string( str0 );
in_string >> itmpe >> ctmpe;
cout << itmpe << " " << ctmpe << endl;
itmpe = lexical_cast<int>(str1);
ctmpe = lexical_cast<char>(str1);
system( "PAUSE" );
return 0;
}
catch(bad_lexical_cast e)
{
cout << e.what() << endl;
cin.get();
}