原文:

std::is tingstream是一个字符串类对象,用于流式传输到不同的变量中,类似的文件也可以流式传输到字符串中。此类的对象使用包含一系列字符的字符串缓冲区。这个字符序列可以作为字符串对象来访问。 头文件:

#include 

1。从带有 std::istingstream 的字符串中流式传输整数

对字符串进行流式处理的一种方法是使用标题中的输入字符串流对象std::isting stream。一旦创建了一个 std::istringstream 对象,就可以使用 ( > > )对字符串进行流式传输和存储。提取操作符将一直读取,直到到达空白或流失败。 以下是标准的说明: :

卡片打印处理机(card print processor 的缩写)

// 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";
}

output: 

1

std::is tingstream对象也可以用作布尔值来确定最后一次提取操作是否失败。如果流中没有更多的字符串,就会发生这种情况,例如,如果流中仍然有更多的字符,那么我们就可以再次对字符串进行流式处理。 提取运算符 > > 将流写入运算符右侧的变量,并返回std::is tingstream对象,因此整个表达式 my_stream > > n 是一个 std::is tingstream 对象,它返回一个布尔值,即如果流是可能的,则返回 true,否则返回 false。 以下是通过上述方式使用std::isting stream的实现:

类型 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 until 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;
}

类型 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;
}

output: 

that stream was successful: 1
that stream was successful: 2
that stream was successful: 3
that stream was not successful!

2。混合类型琴弦t3

在上图中,字符串只包含空格和可以转换为 int 的字符。如果字符串具有混合类型,即它在流中包含不止一种数据类型,则可以使用它,如下所示。 以下是混合类型的标准:行政流: 程序 1:

卡片打印处理机(card print processor 的缩写)

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

output: 

that stream was successful: 1,
that stream was successful: 2,
the stream has failed.

节目 2:

卡片打印处理机(card print processor 的缩写)

// 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';
    }
}

output: 

abc
def
ghi

参考:t2http://www.cplusplus.com/reference/sstream/istringstream/