1.介绍
stringstream 将字符串对象与流相关联,允许从字符串中读取,就好像它是一个流(如 cin)
clear() — 清除流
str() — 获取和设置其内容存在于流中的字符串对象。
运算符 << — 将字符串添加到 stringstream 对象。
运算符 >> — 从 stringstream 对象中读取内容。
2.应用
2.1 计算字符串中的单词数
// CPP program to count words in a string using stringstream.
#include <bits/stdc++.h>
using namespace std;
int countWords(string str)
{
// breaking input into word using string stream
stringstream s(str); // Used for breaking words
string word; // to store individual words
int count = 0;
while (s >> word)
count++;
return count;
}
// Driver code
int main()
{
string s = "geeks for geeks geeks "
"contribution placements";
cout << " Number of words are: " << countWords(s);
return 0;
}
2.2 打印字符串中单个单词的频率
// CPP program to demonstrate use of stringstream
// to count frequencies of words.
#include <bits/stdc++.h>
using namespace std;
void printFrequency(string st)
{
// each word it mapped to it's frequency
map<string, int> FW;
stringstream ss(st); // Used for breaking words
string Word; // To store individual words
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 Ide";
printFrequency(s);
return 0;
}
2.3 使用 Stringstream 从字符串中删除空格
代码一(更优)
// C++ program to remove spaces using stringstream
#include <bits/stdc++.h>
using namespace std;
// Function to remove spaces
string removeSpaces(string str)
{
stringstream ss;
string temp;
// Storing the whole string
// into string stream
ss << str;
// Making the string empty
str = "";
// Running loop till end of stream
while (!ss.eof()) {
// Extracting word by word from stream
ss >> temp;
// Concatenating in the string to be
// returned
str = str + temp;
}
return str;
}
// Driver function
int main()
{
// Sample Inputs
string s = "This is a test";
cout << removeSpaces(s) << endl;
s = "geeks for geeks";
cout << removeSpaces(s) << endl;
s = "geeks quiz is awesome!";
cout << removeSpaces(s) << endl;
s = "I love to code";
cout << removeSpaces(s) << endl;
return 0;
}
代码二
// C++ program to remove spaces using stringstream
// and getline()
#include <bits/stdc++.h>
using namespace std;
// Function to remove spaces
string removeSpaces(string str)
{
// Storing the whole string
// into string stream
stringstream ss(str);
string temp;
// Making the string empty
str = "";
// Running loop till end of stream
// and getting every word
while (getline(ss, temp, ' ')) {
// Concatenating in the string
// to be returned
str = str + temp;
}
return str;
}
// Driver function
int main()
{
// Sample Inputs
string s = "This is a test";
cout << removeSpaces(s) << endl;
s = "geeks for geeks";
cout << removeSpaces(s) << endl;
s = "geeks quiz is awesome!";
cout << removeSpaces(s) << endl;
s = "I love to code";
cout << removeSpaces(s) << endl;
return 0;
}
2.4 C/C+ + 中字符串到数字的转换
将字符串转换为数字有两种常用的方法: 使用 stringstream 类或 sscanf()
stringstream() :这是将数字字符串转换为整数、浮点数或双精度数的简单方法。
以下是使用 stringstream 将 string 转换为 int 的示例程序。
// 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;
}
sscanf()是一个类似于 scanf() 的 C 风格函数。它从字符串而不是标准输入中读取输入。
#include<stdio.h>
int main()
{
const char *str = "12345";
int x;
sscanf(str, "%d", &x);
printf("\nThe value of x : %d", x);
return 0;
}