Need help with stringstream

Started by
0 comments, last by SiCrane 16 years, 9 months ago
I don't really understand what it does or how it works. Here's what my tutorial says about it: "The standard header file <sstream> defines a class called stringstream that allows a string-based object to be treated as a stream. This way we can perform extraction or insertion operations from/to strings, which is especially useful to convert strings to numerical values and vice versa. For example, if we want to extract an integer from a string we can write: string mystr ("1204"); int myint; stringstream(mystr) >> myint; This declares a string object with a value of "1204", and an int object. Then we use stringstream's constructor to construct an object of this type from the string object. Because we can use stringstream objects as if they were streams, we can extract an integer from it as we would have done on cin by applying the extractor operator (>>) on it followed by a variable of type int. After this piece of code, the variable myint will contain the numerical value 1204." Can someone explain stringstream to me?
Advertisement
Are you familiar with fstream? std::stringstream works like fstream except that instead of reading or writing to a file, it reads from writes to a string.

So stringstream(mystr) is like opening a file that contained mystr as its contents.

This topic is closed to new replies.

Advertisement