📜  使用std :: istringstream处理字符串

📅  最后修改于: 2021-05-31 22:11:22             🧑  作者: Mango

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的说明:

程序1:

// 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.

程式2:

// 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/

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”