用標(biāo)準(zhǔn)c++實(shí)現(xiàn)string與各種類型之間的轉(zhuǎn)換
要實(shí)現(xiàn)這個目標(biāo),非stringstream類莫屬。
這個類在頭文件中定義, < sstream>庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進(jìn)行流的輸入、輸出和輸入輸出操作。另外,每個類都有一個對應(yīng)的寬字符集版本。
簡單起見,我主要以stringstream為中心,因?yàn)槊總€轉(zhuǎn)換都要涉及到輸入和輸出操作。
示例1示范怎樣使用一個stringstream對象進(jìn)行從 string到int類型的轉(zhuǎn)換 注意,使用string對象來代替字符數(shù)組。這樣可以避免緩沖區(qū)溢出的危險。而且,傳入?yún)?shù)和目標(biāo)對象的類型被自動推導(dǎo)出來,即使使用了不正確的格式化符也沒有危險。
示例1:
std::stringstream stream;
string result="10000";
int n = 0;
stream << result; stream >> n;//n等于10000
int到string類型的轉(zhuǎn)換
string result;
int n = 12345;
stream << n;
result =stream.str();// result等于"12345"
重復(fù)利用stringstream對象 如果你打算在多次轉(zhuǎn)換中使用同一個stringstream對象,記住再每次轉(zhuǎn)換前要使用clear()方法,在多次轉(zhuǎn)換中重復(fù)使用同一個 stringstream(而不是每次都創(chuàng)建一個新的對象)對象最大的好處在于效率。stringstream對象的構(gòu)造和析構(gòu)函數(shù)通常是非常耗費(fèi)CPU 時間的。經(jīng)試驗(yàn),單單使用clear()并不能清除stringstream對象的內(nèi)容,僅僅是了該對象的狀態(tài),要重復(fù)使用同一個 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("");
}
轉(zhuǎn)換中使用模板 也可以輕松地定義函數(shù)模板來將一個任意的類型轉(zhuǎn)換到特定的目標(biāo)類型。
例如,需要將各種數(shù)字值,如int、long、double等等轉(zhuǎn)換成字符串,要使用以一個string類型和一個任意值t為參數(shù)的to_string()函數(shù)。
to_string()函數(shù)將t轉(zhuǎn)換為字符串并寫入result中。
使用str()成員函數(shù)來獲取流內(nèi)部緩沖的一份拷貝:
示例3:
template void to_string(string & result,const T& t)
{ ostringstream oss;//創(chuàng)建一個流 oss< out_type convert(const in_value & t)
{ stringstream stream; stream<>result;//向result中寫入值 return result; }
這樣使用convert(): double d; string salary; string s=”12.56”; d=convert(s);//d等于12.56 salary=convert(9000.0);//salary等于”9000”
結(jié)論:在過去留下來的程序代碼和純粹的C程序中,傳統(tǒng)的形式的轉(zhuǎn)換伴隨了我們很長的一段時間。但是,如文中所述,基于 stringstream的轉(zhuǎn)換擁有類型安全和不會溢出這樣搶眼的特性,使我們有充足得理由拋棄而使用< sstream>。
當(dāng)然現(xiàn)在還有一個更好的選擇,那就是使用boost庫中的lexical_cast,它是類型安全的轉(zhuǎn)換。
如下例:
#include #include #include #include #include
using namespace std;
using namespace boost;
int main(void)
try
{
//以下是內(nèi)置類型向string轉(zhuǎn)換的解決方案
//lexical_cast優(yōu)勢明顯
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)置類型轉(zhuǎn)換的解決方案
//幾乎和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(str1);
ctmpe = lexical_cast(str1);
system( "PAUSE" );
return 0;
} catch(bad_lexical_cast e)
{ cout << e.what() << endl; cin.get(); }
- C++中string轉(zhuǎn)換為char*類型返回后亂碼問題解決
- 自己模擬寫C++中的String類型實(shí)例講解
- C++中將string類型轉(zhuǎn)化為int類型
- 關(guān)于C++ string和c類型字符數(shù)組的對比
- C++利用stringstream進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換實(shí)例
- 利用C++實(shí)現(xiàn)從std::string類型到bool型的轉(zhuǎn)換
- C++如何通過ostringstream實(shí)現(xiàn)任意類型轉(zhuǎn)string
- 淺談C++中的string 類型占幾個字節(jié)
- C++中的string類型
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(46.全排列)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(46.全排列),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語言實(shí)現(xiàn)BMP圖像處理(直方圖均衡化)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)BMP圖像直方圖均衡化處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
C++實(shí)現(xiàn)讀取特定路徑下文件夾及文件名的方法
這篇文章主要介紹了C++實(shí)現(xiàn)讀取特定路徑下文件夾及文件名的方法,需要的朋友可以參考下2014-07-07

