C++ stringstream類用法詳解
本文主要介紹 C++ 中 stringstream 類的常見用法。
1 概述
<sstream> 定義了三個類:istringstream、ostringstream 和 stringstream,分別用來進(jìn)行流的輸入、輸出和輸入輸出操作。本文以 stringstream 為主,介紹流的輸入和輸出操作。
<sstream> 主要用來進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換,由于 <sstream> 使用 string 對象來代替字符數(shù)組(snprintf方式),就避免緩沖區(qū)溢出的危險;而且,因?yàn)閭魅雲(yún)?shù)和目標(biāo)對象的類型會被自動推導(dǎo)出來,所以不存在錯誤的格式化符的問題。簡單說,相比c庫的數(shù)據(jù)類型轉(zhuǎn)換而言,<sstream> 更加安全、自動和直接。
2 代碼示例
2.1 數(shù)據(jù)類型轉(zhuǎn)換
這里展示一個代碼示例,該示例介紹了將 int 類型轉(zhuǎn)換為 string 類型的過程。示例代碼(stringstream_test1.cpp)如下:
#include <string> #include <sstream> #include <iostream> #include <stdio.h> using namespace std; int main() { stringstream sstream; string strResult; int nValue = 1000; // 將int類型的值放入輸入流中 sstream << nValue; // 從sstream中抽取前面插入的int類型的值,賦給string類型 sstream >> strResult; cout << "[cout]strResult is: " << strResult << endl; printf("[printf]strResult is: %s\n", strResult.c_str()); return 0; }
編譯并執(zhí)行上述代碼,結(jié)果如下:
2.2 多個字符串拼接
本示例介紹在 stringstream 中存放多個字符串,實(shí)現(xiàn)多個字符串拼接的目的(其實(shí)完全可以使用 string 類實(shí)現(xiàn)),同時,介紹 stringstream 的清空方法。
示例代碼(stringstream_test2.cpp)如下:
#include <string> #include <sstream> #include <iostream> using namespace std; int main() { stringstream sstream; // 將多個字符串放入 sstream 中 sstream << "first" << " " << "string,"; sstream << " second string"; cout << "strResult is: " << sstream.str() << endl; // 清空 sstream sstream.str(""); sstream << "third string"; cout << "After clear, strResult is: " << sstream.str() << endl; return 0; }
編譯并執(zhí)行上述代碼,結(jié)果如下:
從上述代碼執(zhí)行結(jié)果能夠知道:
- 可以使用 str() 方法,將 stringstream 類型轉(zhuǎn)換為 string 類型;
- 可以將多個字符串放入 stringstream 中,實(shí)現(xiàn)字符串的拼接目的;
- 如果想清空 stringstream,必須使用 sstream.str(""); 方式;clear() 方法適用于進(jìn)行多次數(shù)據(jù)類型轉(zhuǎn)換的場景。詳見示例2.3。
2.3 stringstream的清空
清空 stringstream 有兩種方法:clear() 方法以及 str("") 方法,這兩種方法有不同的使用場景。str("") 方法的使用場景,在上面的示例中已經(jīng)介紹了,這里介紹 clear() 方法的使用場景。示例代碼(stringstream_test3.cpp)如下:
#include <sstream> #include <iostream> using namespace std; int main() { stringstream sstream; int first, second; // 插入字符串 sstream << "456"; // 轉(zhuǎn)換為int類型 sstream >> first; cout << first << endl; // 在進(jìn)行多次類型轉(zhuǎn)換前,必須先運(yùn)行clear() sstream.clear(); // 插入bool值 sstream << true; // 轉(zhuǎn)換為int類型 sstream >> second; cout << second << endl; return 0; }
編譯并執(zhí)行上述代碼,結(jié)果如下:
注意:在本示例涉及的場景下(多次數(shù)據(jù)類型轉(zhuǎn)換),必須使用 clear() 方法清空 stringstream,不使用 clear() 方法或使用 str("") 方法,都不能得到數(shù)據(jù)類型轉(zhuǎn)換的正確結(jié)果。下圖分別是未使用 clear() 方法、使用 str("") 方法時的運(yùn)行結(jié)果:
到此這篇關(guān)于C++ stringstream類用法詳解的文章就介紹到這了,更多相關(guān)C++ stringstream類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實(shí)現(xiàn)求解最小公倍數(shù)的算法示例
這篇文章主要為大家介紹了C語言如何實(shí)現(xiàn)求解任意兩個正整數(shù)的最小公倍數(shù),文中采用了窮舉法和定理法。感興趣的小伙伴快來跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12Qt使用QCustomPlot的實(shí)現(xiàn)示例
QCustomPlot是一個基于Qt C++的圖形庫,用于繪制和數(shù)據(jù)可視化,并為實(shí)時可視化應(yīng)用程序提供高性能服務(wù),本文主要介紹了Qt使用QCustomPlot的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-01-01C++數(shù)據(jù)結(jié)構(gòu)關(guān)于棧迷宮求解示例
這篇文章主要為大家介紹了C++數(shù)據(jù)結(jié)構(gòu)關(guān)于棧的迷宮求解示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11