將C++數字類型轉換成字串__C++

來源:互聯網
上載者:User
#include <iostream>#include <sstream>#include <string>using 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;}
實現這個目標,非stringstream類莫屬。

這個類在標頭檔中定義, < sstream>庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進行流的輸入、輸出和輸入輸出操作。另外,每個類都有一個對應的寬字元集版本。

簡單起見,我主要以stringstream為中心,因為每個轉換都要涉及到輸入和輸出操作。

樣本1示範怎樣使用一個stringstream對象進行從 string到int類型的轉換 注意,使用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 void to_string(string & result,const T& t)

{ ostringstream oss;//建立一個流 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”

結論:在過去留下來的程式碼和純粹的C程式中,傳統的形式的轉換伴隨了我們很長的一段時間。但是,如文中所述,基於 stringstream的轉換擁有型別安全和不會溢出這樣搶眼的特性,使我們有充足得理由拋棄而使用< sstream>。

當然現在還有一個更好的選擇,那就是使用boost庫中的lexical_cast,它是型別安全的轉換。

如下例:
複製代碼 代碼如下:
#include #include #include #include #include

 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(ival) + lexical_cast(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(str1);

ctmpe = lexical_cast(str1);

system( "PAUSE" );

return 0;

} catch(bad_lexical_cast e) { cout << e.what() << endl; cin.get(); }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.