Thread: Understanding istringstream

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40

    Understanding istringstream

    Consider I have a vector of strings and then I use an istringstream to read each word of each element in the vector, why do I nescessarily use an istringstream?

    This is the code that does what I just described (I think)..
    Code:
    for(auto &elem : svec)    {
            istringstream strm(elem);
            while (strm >> word)
                //Magic
        }
    What would be the equivalent of using something else than an istringstream in this scenario and how does the istringstream work?
    Thanks in advance!!
    PS. point out any error in the code or improvements if you want..
    "Derp, derp, derp" - Me

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    istringstream is a stream that instead of reading from a file, reads from a supplied string. Otherwise, it is equivalent to reading from files (since we dealing with the stream interface).
    You don't necessarily have to use it here. The other alternative would be to to find each space with a find function, then extract it using a substring function. This is another way of doing it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Just like std::cin works actually.
    Only difference is that it is reading from the standrd input and this is reading from a string supplied by you.

    Well, you could find the location of each whitespace and construct new strings based on the ranges you find.
    But there is going to be a lot of edge cases.
    So, you're better of using string streams.

    (If you know C, consider this similar in functionality to sscanf .)

    [EDIT:..nevermind, same info on above post]

  4. #4
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    Aah, that was what I thought, that istringstream is basically fstream, just specifically for strings. (If it's that what you meant) I just wasnt sure. Thank you very much!
    "Derp, derp, derp" - Me

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Iceboxes View Post
    Aah, that was what I thought, that istringstream is basically fstream, just specifically for strings.
    Not quite. istringstream is a specialised istream. fstream and ifstream are other distinct specialisations of istream.

    In other words, fstream and istringstream are siblings (in the sense of both being specialisations of istream). But istringstream is not an fstream (nor vice versa).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Iceboxes View Post
    Aah, that was what I thought, that istringstream is basically fstream, just specifically for strings. (If it's that what you meant) I just wasnt sure. Thank you very much!
    They're all streams.
    fstream is a stream for files.
    stringstream is a stream for strings.
    std::cout, std::cin are streams for reading and writing to standard input/output (usually the screen and keyboard, but does not necessarily need to be).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    that istringstream is basically fstream, just specifically for strings
    I meant as in how they work Or do they work differently? If I were to take a fstream, make it into a string and then apply that string to istringstream, I would basically have the same stream as I originally did, just a istringstream instead of a fstream?
    Last edited by Iceboxes; 03-06-2014 at 08:50 AM.
    "Derp, derp, derp" - Me

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you take the entire contents of a file, put it into a string and make a stringstream from that, it would behave exactly the same as if you were reading directly from the file, yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf vs istringstream
    By KIBO in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2010, 03:00 AM
  2. istringstream
    By wind_lark in forum C++ Programming
    Replies: 6
    Last Post: 08-31-2006, 03:47 PM
  3. istringstream
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2004, 10:10 PM
  4. istringstream.get me something!!
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2003, 06:02 PM
  5. Istringstream fail
    By pianorain in forum C++ Programming
    Replies: 2
    Last Post: 03-09-2003, 11:28 PM