使用stringstream對象簡化類型轉(zhuǎn)換
C++標準庫中的<sstream>提供了比ANSI C的<stdio.h>更高級的一些功能,即單純性、類型安全和可擴展性。在本文中,我將展示怎樣使用這些庫來實現(xiàn)安全和自動的類型轉(zhuǎn)換。
為什么要學習
如果你已習慣了<stdio.h>風格的轉(zhuǎn)換,也許你首先會問:為什么要花額外的精力來學習基于<sstream>的類型轉(zhuǎn)換呢?也許對下面一個簡單的例子的回顧能夠說服你。假設你想用sprintf()函數(shù)將一個變量從int類型轉(zhuǎn)換到字符串類型。為了正確地完成這個任務,你必須確保證目標緩沖區(qū)有足夠大空間以容納轉(zhuǎn)換完的字符串。此外,還必須使用正確的格式化符。如果使用了不正確的格式化符,會導致非預知的后果。下面是一個例子:
int n=10000;
chars[10];
sprintf(s,”%d”,n);// s中的內(nèi)容為“10000”
到目前為止看起來還不錯。但是,對上面代碼的一個微小的改變就會使程序崩潰:
int n=10000;
char s[10];
sprintf(s,”%f”,n);// 看!錯誤的格式化符
在這種情況下,程序員錯誤地使用了%f格式化符來替代了%d。因此,s在調(diào)用完sprintf()后包含了一個不確定的字符串。要是能自動推導出正確的類型,那不是更好嗎?
進入stringstream
由于n和s的類型在編譯期就確定了,所以編譯器擁有足夠的信息來判斷需要哪些轉(zhuǎn)換。<sstream>庫中聲明的標準類就利用了這一點,自動選擇所必需的轉(zhuǎn)換。而且,轉(zhuǎn)換結(jié)果保存在stringstream對象的內(nèi)部緩沖中。你不必擔心緩沖區(qū)溢出,因為這些對象會根據(jù)需要自動分配存儲空間。
你的編譯器支持<sstream>嗎?
<sstream>庫是最近才被列入C++標準的。(不要把<sstream>與標準發(fā)布前被刪掉的<strstream>弄混了。)因此,老一點的編譯器,如GCC2.95,并不支持它。如果你恰好正在使用這樣的編譯器而又想使用<sstream>的話,就要先對它進行升級更新。
<sstream>庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進行流的輸入、輸出和輸入輸出操作。另外,每個類都有一個對應的寬字符集版本。簡單起見,我主要以stringstream為中心,因為每個轉(zhuǎn)換都要涉及到輸入和輸出操作。
注意,<sstream>使用string對象來代替字符數(shù)組。這樣可以避免緩沖區(qū)溢出的危險。而且,傳入?yún)?shù)和目標對象的類型被自動推導出來,即使使用了不正確的格式化符也沒有危險。
string到int的轉(zhuǎn)換
string result=”10000”;
int n=0;
stream<<result;
stream>>n;//n等于10000
重復利用stringstream對象
如果你打算在多次轉(zhuǎn)換中使用同一個stringstream對象,記住再每次轉(zhuǎn)換前要使用clear()方法;
在多次轉(zhuǎn)換中重復使用同一個stringstream(而不是每次都創(chuàng)建一個新的對象)對象最大的好處在于效率。stringstream對象的構(gòu)造和析構(gòu)函數(shù)通常是非常耗費CPU時間的。
在類型轉(zhuǎn)換中使用模板
你可以輕松地定義函數(shù)模板來將一個任意的類型轉(zhuǎn)換到特定的目標類型。例如,需要將各種數(shù)字值,如int、long、double等等轉(zhuǎn)換成字符串,要使用以一個string類型和一個任意值t為參數(shù)的to_string()函數(shù)。to_string()函數(shù)將t轉(zhuǎn)換為字符串并寫入result中。使用str()成員函數(shù)來獲取流內(nèi)部緩沖的一份拷貝:
template<class T>
void to_string(string & result,const T& t)
{
ostringstream oss;//創(chuàng)建一個流
oss<<t;//把值傳遞如流中
result=oss.str();//獲取轉(zhuǎn)換后的字符轉(zhuǎn)并將其寫入result
}
這樣,你就可以輕松地將多種數(shù)值轉(zhuǎn)換成字符串了:
to_string(s1,10.5);//double到string
to_string(s2,123);//int到string
to_string(s3,true);//bool到string
可以更進一步定義一個通用的轉(zhuǎn)換模板,用于任意類型之間的轉(zhuǎn)換。函數(shù)模板convert()含有兩個模板參數(shù)out_type和in_value,功能是將in_value值轉(zhuǎn)換成out_type類型:
template<class out_type,class in_value>
out_type convert(const in_value & t)
{
stringstream stream;
stream<<t;//向流中傳值
out_type result;//這里存儲轉(zhuǎn)換結(jié)果
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”
結(jié)論
在過去留下來的程序代碼和純粹的C程序中,傳統(tǒng)的<stdio.h>形式的轉(zhuǎn)換伴隨了我們很長的一段時間。但是,如文中所述,基于stringstream的轉(zhuǎn)換擁有類型安全和不會溢出這樣搶眼的特性,使我們有充足得理由拋棄<stdio.h>而使用<sstream>。<sstream>庫還提供了另外一個特性—可擴展性。你可以通過重載來支持自定義類型間的轉(zhuǎn)換。
一些實例:
stringstream通常是用來做數(shù)據(jù)轉(zhuǎn)換的。
相比c庫的轉(zhuǎn)換,它更加安全,自動和直接。
例子一:基本數(shù)據(jù)類型轉(zhuǎn)換例子 int轉(zhuǎn)string
#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"
}
例子三:再進行多次轉(zhuǎn)換的時候,必須調(diào)用stringstream的成員函數(shù)str("")
#include <sstream>
#include <iostream>
int main()


{
std::stringstream stream;
int first, second;
stream<< "456"; //插入字符串
stream >> first; //轉(zhuǎn)換成int
std::cout << first << std::endl;
// This copies an empty string into ss, erasing the
// previous contents.
ss << "";
// This clears the 'eof' flag. Otherwise, even after
// writing new data to ss we wouldn't be able to
// read from it.
ss.clear(); stream << true; //插入bool值
stream >> second; //提取出int
std::cout << second << std::endl;
}
原文地址:
http://www.builder.com.cn/2003/0304/83250.shtml
http://www.shnenglu.com/alantop/archive/2007/07/10/27823.html
----------------------------------------------------------華麗麗的分割線-------------------------------------------------------------------------------
posted on 2012-07-31 08:15
風輕云淡 閱讀(378)
評論(0) 編輯 收藏 引用 所屬分類:
C++