C++ stringstream is a stream class on strings. This allows us to take a string and treat it as if it were a stream object, like cin
, or cout
.
C ++ stringstream是字符串上的流类。 这使我们可以采用字符串并将其视为流对象 ,例如cin
或cout
。
Due to this, we can perform operations like indirection and out-direction, using <<
and >>
.
因此,我们可以使用<<
和>>
执行诸如间接和向外等操作。
Let’s look at how we can use this class to parse strings easily.
让我们看看如何使用此类轻松解析字符串。
We can convert a string, and create a stringstream
object.
我们可以转换一个字符串,并创建一个stringstream
对象。
But before that, we need to include the relevant header file, which we can get at the
header file.
但是在此之前,我们需要包含相关的头文件,可以在
头文件中获得该文件。
#include
To create the object, it’s really simple. The relevant type is stringstream
, and we need to input a string into the constructor.
创建对象非常简单。 相关的类型是stringstream
,我们需要在构造函数中输入一个字符串。
#include
#include
#include
// We use the std:: namespace,
// otherwise we need to specify the
// stringstream using std::stringstream
using namespace std;
int main() {
// Input string
string input_string = "Hello from JournalDev";
// Create the stringstream from the input string
stringstream ss(input_string);
cout << "Created a stringstream object\n";
return 0;
}
Output
输出量
Created a stringstream object
Right now, we’ve not made use of the stringstream object yet. Let’s now look at how we can read or write from the stream.
现在,我们还没有使用stringstream对象。 现在让我们看一下如何从流中读取或写入。
Similar to cin
, we can do this using the >>
operator, to read a string from the stringstream.
与cin
类似,我们可以使用>>
运算符执行此操作,以从stringstream中读取字符串。
When we initialize the stringstream object using a string, the contents of the string will be treated as a stream-like object.
当我们使用字符串初始化stringstream对象时,该字符串的内容将被视为类似于流的对象。
string word;
// Read from the stringstream into word
ss >> word;
cout << word << endl;
So, whenever we read from the stream, we will get a string! The advantage of this method is that it automatically parses delimiters like spaces, commas, etc.
因此,无论何时从流中读取,我们都会得到一个字符串! 这种方法的优点是它会自动解析定界符,例如空格,逗号等。
We can use stringstream
to parse our input string and get an array of strings, after parsing with delimiters!
在使用定界符进行解析之后,我们可以使用stringstream
解析我们的输入字符串并获取字符串数组!
#include
#include
#include
// We use the std:: namespace,
// otherwise we need to specify the
// stringstream using std::stringstream
using namespace std;
int main() {
// Input string
string input_string = "Hello from JournalDev";
// Create the stringstream from the input string
stringstream ss(input_string);
cout << "Created a stringstream object\n";
// To store the words that we get back from the stringstream
string words[5];
string buffer;
int i = 0;
while (ss >> buffer) {
words[i] = buffer;
cout << "Buffer: " << buffer << endl;
i++;
}
// Print the tokenized set of strings from words[]
cout << "Printing the tokenized array of strings\n";
for (int j = 0; j < i; j++) {
cout << words[j] << endl;
}
return 0;
}
Here, we check if our stream is empty, using ss >> buffer
. If ss
is not empty, it will write to buffer
and go into the body of the loop. Otherwise, it will simply exit from the loop.
在这里,我们使用ss >> buffer
检查流是否为空。 如果ss
不为空,它将写入buffer
并进入循环主体。 否则,它将仅退出循环。
Since our input string contains spaces, the output will be a tokenized list of strings, after removing the spaces.
由于我们的输入字符串包含空格,因此删除空格后,输出将是字符串的标记化列表。
Output
输出量
Created a stringstream object
Buffer: Hello
Buffer: from
Buffer: JournalDev
Printing the tokenized array of strings
Hello
from
JournalDev
Similar to reading from the stream, we can also write into the stream, just like cout <<
!
类似于从流中读取,我们也可以写入流中,就像cout <<
!
Unlike our previous example, instead of initializing the stringstream
object using the constructor, we can also simply declare it, and then write to it later!
与我们之前的示例不同,我们无需使用构造函数来初始化stringstream
对象,还可以简单地声明它,然后再写入它!
// Input string
string input_string = "Hello from JournalDev";
// Create an empty stringstream object
stringstream ss;
ss << input_string;
So, we will get the same output as before, but we have also written into our stream now!
因此,我们将获得与以前相同的输出,但现在我们也已将其写入流中!
#include
#include
#include
// We use the std:: namespace,
// otherwise we need to specify the
// stringstream using std::stringstream
using namespace std;
int main() {
// Input string
string input_string = "Hello from JournalDev";
// Create the stringstream from the input string
stringstream ss;
cout << "Created a stringstream object\n";
// To store the words that we get back from the stringstream
string words[5];
string buffer;
ss << input_string;
int i = 0;
while (ss >> buffer) {
words[i] = buffer;
cout << "Buffer: " << buffer << endl;
i++;
}
// Print the tokenized set of strings from words[]
cout << "Printing the tokenized array of strings\n";
for (int j = 0; j < i; j++) {
cout << words[j] << endl;
}
return 0;
}
Output
输出量
Created a stringstream object
Buffer: Hello
Buffer: from
Buffer: JournalDev
Printing the tokenized array of strings
Hello
from
JournalDev
Suppose you want to erase whatever content is currently on ss
, there is a simple function for that:
假设您要擦除ss
上当前存在的任何内容,则有一个简单的功能:
ss.clear();
This simply clears whatever content is there in the buffer.
这只是清除缓冲区中存在的任何内容。
#include
#include
#include
// We use the std:: namespace,
// otherwise we need to specify the
// stringstream using std::stringstream
using namespace std;
int main() {
// Input string
string input_string = "Hello from JournalDev";
// Create the stringstream from the input string
stringstream ss;
cout << "Created a stringstream object\n";
// To store the words that we get back from the stringstream
string words[5];
string buffer;
ss << input_string;
int i = 0;
while (ss >> buffer) {
words[i] = buffer;
cout << "Buffer: " << buffer << endl;
i++;
}
// Print the tokenized set of strings from words[]
cout << "Printing the tokenized array of strings\n";
for (int j = 0; j < i; j++) {
cout << words[j] << endl;
}
ss.clear();
if (ss >> buffer) {
cout << "Stream is not empty: Contains " << buffer << endl;
}
else {
cout << "Stream is empty!\n";
}
return 0;
}
Output
输出量
Created a stringstream object
Buffer: Hello
Buffer: from
Buffer: JournalDev
Printing the tokenized array of strings
Hello
from
JournalDev
Stream is empty!
We check if our stream is actually empty, using if (ss >> buffer)
. Since the check fails, we have indeed cleared our stringstream!
我们使用if (ss >> buffer)
检查流实际上是否为空。 由于检查失败,因此我们确实清除了字符串流!
In this article, we learned about how we could use the stringstream object in C++, to easily parse strings!
在本文中,我们学习了如何在C ++中使用stringstream对象轻松解析字符串!
For similar topics on C++, do visit our C++ tutorials page.
有关C ++的类似主题,请访问我们的C ++教程页面。
翻译自: https://www.journaldev.com/36971/c-plus-plus-stringstream