A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin).
接口函數(shù)
clear() — to clear the stream
str() — to get and set string object whose content is present in stream.
operator << — add a string to the stringstream object.
operator >> — read something from the stringstream object,
應(yīng)用
- 統(tǒng)計(jì)字符串中的單詞數(shù)目
- 統(tǒng)計(jì)字符串中的詞頻
- 去除字符串中的空格
- 字符串轉(zhuǎn)換為數(shù)字
示例來(lái)源
// A program to demonstrate the use of stringstream
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string s = "12345";
// object from the class stringstream
stringstream geek(s);
// The object has the value 12345 and stream
// it to the integer x
int x = 0;
geek >> x;
// Now the variable x holds the value 12345
cout << "Value of x : " << x;
return 0;
}