CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2013
    Posts
    12

    understanding istringstream in c++

    Hi,
    I am new to c++ and in process fo understading and learning string class.

    I am working on istringstream
    Code:
        std::istringstream iss;
        std::string value="32 40 50 80 902";
    
        iss.str (value); //what does this do???
        for(int i=0;i<5;i++){
            cout<< " " <<i<<"\n";
            int val;
            iss >> val;
    I get output as 32 40 50 80 902
    That is; string has been split up by space.

    Code:
     std::string value="32,40,50,80,902";
    If I replace space by comma, the output is 32 and all 0 there after.

    I cannot understand how it actually works.

    PS: Apolologies, if I have posted question in wrong forum. Kindly provide me so not repeat mistake in future.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: understanding istringstream in c++

    You might get a better idea from this

    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    istringstream iss;
    
    string value = "32,40,50,80,902";
    
        iss.str (value); //what does this do???
        for (int i = 0; i < 5; i++) {
    	string val;
            iss >> val;
    	cout << i << endl << val <<endl;
        }
    
         return 0;
    }
    which outputs
    Code:
    0
    32,40,50,80,902
    1
    
    2
    
    3
    
    4
    As you can see, for index 0 it outputs the whole string rather than just the first part. This is because iss.str(value) creates a string stream called iss consisting of the string value. iss >> val extracts the next element from the iss stream and puts it into val. Elements in a character stream are considered to be separated by 'white space' which is basically space, tab and newline characters (see http://msdn.microsoft.com/en-us/libr.../e9a023cx.aspx). So when the elements are separated by a ',' this is not a 'white-space' character so the whole string is read. When the elements are separated by a ' ' this is a white space character so the next element obtained by iss >> val is the next number in the string.

    So

    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    istringstream iss;
    
    string value = "32 40 50 80 902";
    
        iss.str (value); //what does this do???
        for (int i = 0; i < 5; i++) {
    	string val;
            iss >> val;
    	cout << i << endl << val <<endl;
        }
    
        return 0;
    }
    produces
    Code:
    0
    32
    1
    40
    2
    50
    3
    80
    4
    902
    In your original code val was of type int so iss >> val wa trying to read an integer from the stream. When the values were separated by a ' ' this worked as each loop iteration produced the next number. However when separated by a ',' the first extration produced the whole string which is not a number as it contains ','s. So trying to extract an integer produces the first valid number from the stream (32) and then the stream is set to 'bad' because it tries to read a number and found ',' which is not valid. So further attempts to read from the stream fail until the stream state is reset to good.

    Have a look at
    http://www.cplusplus.com/reference/s.../stringstream/

    You might also find these of interest
    http://www.cplusplus.com/doc/
    http://www.learncpp.com/
    Last edited by 2kaud; November 7th, 2013 at 05:51 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Oct 2013
    Posts
    12

    Re: understanding istringstream in c++

    Thank you for such a great explanation and helpful URLs.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured