C/C++ STL容器之stringstream字串流

來源:互聯網
上載者:User

 

輸入輸出的標頭檔 <iostream>
string流的標頭檔 <sstream>
檔案流的標頭檔   <fstream>

stringstream的用法

1.利用輸入輸出做資料轉換
stringstream ss_stream;
ss_stream << i; // 將int輸入資料流中
ss_stream >> str; // 將ss_stream中的數值輸出到str中

//注意:如果做多次資料轉換;必須調用clear()來設定轉換模式
ss_stream << "456";
ss_stream >> i; // 首先將字串轉換為int
ss_stream.clear();
ss_stream << true;
ss_stream >> i; // 然後將bool型轉換為int;假如之前沒有做clear,那麼i會出錯

//運行clear的結果
i = 456
i = 1
//沒有運行clear的結果
i = 456
i = 8800090900

 

 

2.支援char*的輸入和輸出
char sz_buf[20];
ss_stream << 8888;
ss_stream >> sz_buf; // 直接將數輸出到sz_buf字元數組中

 

3.來儲存可變資料的列表
stringstream ss_stream;
ss_stream << "字串一" << endl;
ss_stream << "字串二" << endl;
ss_stream << "字串三" << endl;
ss_stream << "字串四" << endl;
ss_stream << "字串五" << endl;

char buffer[100];
while ( ss_stream.getline(buffer, sizeof(buffer))
{
printf("msg=%s\n", buffer);
}
ss_stream("");// 釋放字串流中的資源

// 或者用string來接收
stringstream ss_stream;
string stemp;
while ( getline(ss_stream, stemp) )
{
task_download(stemp.c_str(), relate.c_str());
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.