std::istringstream是一个字符串类对象,用于将字符串流式传输到不同的变量中,类似的文件也可以流式传输到字符串。此类的对象使用包含字符序列的字符串缓冲区。这个字符序列可以作为字符串对象访问。
头文件:
#include
1. 使用 std::istringstream 从字符串流式传输整数
流式传输字符串的一种方法是使用来自标头的输入字符串流对象std::istringstream 。一旦创建了std::istringstream对象,就可以使用提取运算符( >> ) 流式传输和存储字符串。提取运算符将一直读取直到到达空格或直到流失败。
下面是std::istringstream 的图示:
// C++ program to illustrate std::istringstream
#include
#include
#include
using std::istringstream;
using std::string;
using std::cout;
// Driver Code
int main()
{
// Input string
string a("1 2 3");
// Object class of istringstream
istringstream my_stream(a);
// Variable to store number n
int n;
// Stream a number till white space
// is encountered
my_stream >> n;
// Print the number
cout << n << "\n";
}
1
std::istringstream对象也可以用作布尔值来确定上次提取操作是否失败。如果流中没有更多字符串,就会发生这种情况,例如,如果流仍有更多字符,那么我们可以再次流式传输该字符串。
提取运算符>>的流写入到在运算符和返回的std :: istringstream对象的右侧变量,所以整个表达my_stream >> n是一个std :: istringstream对象,它返回一个布尔值,即,如果真流否则可能返回false。
以下是以上述方式使用std::istringstream的实现:
Type 1
// C++ program to illustrate std::istringstream
#include
#include
#include
using std::istringstream;
using std::string;
using std::cout;
// Driver Code
int main()
{
// Input string
string a("1 2 3");
// Object class of istringstream
istringstream my_stream(a);
// Variable to store number n
int n;
// Testing to see if the stream was
// successful and printing results.
while (my_stream) {
// Streaming untill space is
// encountered
my_stream >> n;
// If my_stream is not empty
if (my_stream) {
cout << "That stream was successful: "
<< n << "\n";
}
// Else print not successful
else {
cout << "That stream was NOT successful!"
<< "\n";
}
}
return 0;
}
Type 2
// C++ program to illustrate std::istringstream
#include
#include
#include
using std::istringstream;
using std::string;
using std::cout;
// Driver Code
int main()
{
// Input string
string a("1 2 3");
// Object class of istringstream
istringstream my_stream(a);
// Variable to store number n
int n;
// Testing to see if the stream was
// successful and printing results.
while (my_stream >> n) {
cout << "That stream was successful: "
<< n << "\n";
}
cout << "That stream was NOT successful!"
<< "\n";
return 0;
}
That stream was successful: 1
That stream was successful: 2
That stream was successful: 3
That stream was NOT successful!
2. 混合类型的字符串
在上图中,字符串仅包含可以转换为 int 的空格和字符。如果字符串具有混合类型,即,它在流中包含不止一种数据类型,则可以如下图所示使用它。
下面是混合类型的std::istringstream的说明:
方案一:
// C++ program to illustrate std::istringstream
// when string has integer followed by character
#include
#include
#include
using std::istringstream;
using std::string;
using std::cout;
// Driver Code
int main()
{
// Input string
string str("1, 2, 3");
// Object class of istringstream
istringstream my_stream(str);
// Variable to store the number n
// and character ch
char c;
int n;
// Traverse till input stream is valid
while (my_stream >> n >> c) {
cout << "That stream was successful: "
<< n << " " << c << "\n";
}
cout << "The stream has failed."
<< "\n";
return 0;
}
That stream was successful: 1,
That stream was successful: 2,
The stream has failed.
方案二:
// C++ program to illustrate std::istringstream
// to tokenize the string
#include
#include
#include
using std::istringstream;
using std::string;
using std::cout;
// Driver Code
int main()
{
// Input string
string str("abc, def, ghi");
// Object class of istringstream
istringstream my_stream(str);
// To store the stream string
string token;
size_t pos = -1;
// Traverse till stream is valid
while (my_stream >> token) {
// If ',' is found then tokenize
// the string token
while ((pos = token.rfind(','))
!= std::string::npos) {
token.erase(pos, 1);
}
// Print the tokenize string
cout << token << '\n';
}
}
abc
def
ghi
参考: http : //www.cplusplus.com/reference/sstream/istringstream/
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live