梳理下istringstream ostringstream stringstream的關係
今天在練習面試題37. 序列化二叉樹的時候,需要用到istringstream
,ostringstream
這兩個類,不太熟悉,查找相關文檔,記錄下來。
在頭文件<sstream>中定義了三個字符串流,分別是:
- istringstream:輸入字符串流,是一個類
- ostringstream:輸出字符串流,是一個類
- stringstream:輸入/輸出字符串流,是一個類
他們的繼承關係如下圖所示:
istringstream,此類的對象有一個字符串緩衝區,字符串緩衝區包含了一個字符序列。 可以使用成員方法str直接訪問此字符序列,得到字符串對象string。重載的是右移運算符>>,提取格式化的輸入,每次向運算符>>左邊的變量輸入一個單詞,以空格爲分隔符。
std::istringstream::str
string str() const;
void str (const string& s);
例子:
// istringstream::str
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::istringstream
int main () {
std::istringstream iss;
std::string strvalues = "32 240 2 1450";
iss.str (strvalues); // 將字符串strvalues複製到iss流中
for (int n=0; n<4; n++)
{
int val;
iss >> val; // 每次輸出一個數字,空格爲分隔符
std::cout << val << '\n';
}
std::cout << "Finished writing the numbers in: ";
std::cout << iss.str() << '\n'; // 通過str方法獲取iss中的字符串
return 0;
}
編譯和運行結果:
$ g++ 05_istringstream.cpp -o 05_istringstream.o
$ ./05_istringstream.o
32
240
2
1450
Finished writing the numbers in: 32 240 2 1450
ostringstream,此類的對象也有一個字符串緩衝區,字符串緩衝區包含了一個字符序列。 可以使用成員方法str直接訪問此字符序列,得到字符串對象string。但是重載的是左移運算符<<,插入格式化的輸出,將運算符<<左邊的變量輸入到ostringstream對象中。
std::ostringstream::str
string str() const;
void str (const string& s);
例子:
// ostringstream
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::ostringstream
int main () {
std::ostringstream oss;
oss << "string " << 100 << ' ' << 3.14; // 向oss對象輸入string,int,char,float類型的數據
std::string s = oss.str(); // 獲取oss對象中的字符串
std::cout << s << '\n';
return 0;
}
編譯和運行結果:
$ vim 06_ostringstream.cpp
$ g++ 06_ostringstream.cpp -o 06_ostringstream.o -std=c++11
$ ./06_ostringstream.o
string 100 3.14
stringstream,將字符串對象與流相關聯,使您可以像對待流一樣讀取字符串(如cin)。 可以使用成員方法str直接訪問此字符序列,得到字符串對象string。這個類將右移運算符和**左移運算符<<**都進行了重載。
基本的方法如下:
clear() — 清除流中的數據
str() — 獲得或設置流中字符串的內容,這是一個重載函數,有兩個不同的調用方法
operator << — 向stringstream對象添加一個字符串
operator >> —從stringstream對象讀取一些內容*
std::stringstream::str中str()方法:
string str() const;
void str (const string& s);
stringstream的例子:統計一個字符串中的單詞數量,並打印每個單詞的頻率
輸入:geeks for geeks geeks contribution placements
輸出:
Number of words are: 6
contribution -> 1
for -> 1
geeks -> 3
placements -> 1
// using stringstream.
#include <iostream>
#include <sstream>
#include <string>
#include <map>
using namespace std;
// 統計單詞數量
int countWords(string str)
{
stringstream s(str); // 以空格爲分隔符,分割每個單詞
string word; // 存儲單個的單詞
int count = 0;
while (s >> word)
count++;
return count;
}
// 統計每個單詞的詞頻
void printFrequency(string st)
{
map<string, int> FW; // map保存每個單詞的詞頻
stringstream ss(st); // 分割每個單詞
string Word; // 保存每個單詞
while (ss >> Word)
FW[Word]++;
map<string, int>::iterator m;
for (m = FW.begin(); m != FW.end(); m++)
cout << m->first << " -> "
<< m->second << "\n";
}
// Driver code
int main()
{
string s = "geeks for geeks geeks contribution placements";
cout << " Number of words are: " << countWords(s) << endl;
printFrequency(s);
return 0;
}
編譯和運行結果:
$ g++ 07_stringstream.cpp -o 07_stringstream.o -std=c++11
$ ./07_stringstream.o
Number of words are: 6
contribution -> 1
for -> 1
geeks -> 3
placements -> 1