About the use of StringStream

Source: Internet
Author: User
Tags stdin

In C + + programming, there is a less common, but actually very useful input conversion tool class called StringStream. Say it's not so common, it's because it seems like the general C + + combat project isn't going to use it very much, and it's useful because it's really handy when converting stdin input into variables of various types, and second, because it's really useful in some situations. What occasion. such as programming training sites like Leetcode and Hackerrank. They are all a problem, let you write algorithm implementation. When you finish writing, embed your code directly into the main program, and then use several files as input, and the detection output (STDOUT) is consistent with the target file (that is, the expected result). This enables the automation of code checking. It looks like this:

Cat Input_001.txt | Your_program > Output_001.txt
diff output_001.txt Expected_001.txt
Above "Cat Input_001.txt | Your_program "is to pass the content of the input_001.txt as stdin input to the Your_program, while the standard input needs to be processed in Your_program." The use of this is usually cin. The Getline () function gets the content from the standard input one line at a row, and then makes further processing.

When you get to the contents of a row, the line may consist of several parts, and each part may want to be treated as a string or an int number or a double. So how do you turn the contents of a string (that is, this line) into different types of variables that you would like to have? It's going to take a stringstream. For specific syntax use, see the following instance program. In this program, we assume that the standard input comes with at least 4 parts per line, the first 2 is int, the 3rd is double, and part 4 is an ordinary string.

#include <string>
#include <sstream>
#include <iostream>
using namespace std;

int main ()
{
    string line;
    while (!cin.eof ()) {
        int i1 = 0; 
        int i2 = 0;
        Double d3 = 0.1;
        String S4 = "Hello";
        
        Getline (cin, line);
        if (line.size () = = 0) break;
            
        StringStream SS (line);
        SS >> I1 >> i2 >> D3 >> S4;

        cout << "i1 = << i1 <<", i2 = "<< i2 <<", D3 = "<< D3 <<", S4 = "<< ; S4 << Endl;
        
    }
    return 0;
}
At this point, if the input is strictly in the order of the int int double string above, then all okay, our program naturally can get the correct 4 variables. But what happens if the input file is a string such as "ABC", but wants to be parsed into an int or double. Please see the following input.txt file:

  9.876
ABC   def   xyz      9.876
1.20 2.10     ABC  200     0.99
So, the output is this:

I1 = I2 =, D3 = 9.876, S4 = I1 =
0,   i2 = 0,   d3 = 0.1,   s4 = Hello
i1 = 1,   i2 = 0,   D3 = 0.1,   s4 = Hello I1 = I2 = =, D3 =   0.99
As seen from above, in some cases the parsing is indeed unsuccessful. But the above looked a bit messy, the author specially summed up a table as follows:



The "archetype" here refers to the type of "image" that appears in the Input.txt file, that is, in stdin, and the "turn" here refers to the type of variable that receives the result of the resolution in the C + + program that is required to implement it.

Although there are indeed some errors, it is possible to easily convert the standard input to the target variable by StringStream.

This article is just a brief introduction to the basic use of StringStream, which seems to be sufficient in general. If you have more requests, please check the StringStream manual: Http://en.cppreference.com/w/cpp/io/basic_stringstream

Finish

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.