Stringstream

I recently started playing around with stringstream. From my understanding (which could be wrong) it acts like a string container but with some extra functions associate with the container.

Below is some code that converts a char to an int

I know it works, my question is why it works.

This is what I think is happening.

*The program creates a stringstream container called str.

*a value of '1' gets put into the container with str << '1';

*The program then creates an integer variable called x.

*Then some how (as if by magic) the stringstream converts the char to an integer.


My problem is I don't understand how stringstream does it's magic.

I've found other ways that I understand how they work, like x = '1' - '0'

but from what I've read people say it's C style implementation instead of C++ and I'd like to understand the C++ way.


Does anybody have any reference on how stringstream converts values to different data types. I read the stringstream article on this site but it didn't really explain what sort of limitations stringstream has on data type conversions (if there are any) and

http://www.cplusplus.com/reference/sstream/stringstream/
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

Does the stringstream operator >> basically perform the C style x = '1' - '0' automatically when you use the operator?

I think i sort of worked my way through understanding this by writing it out but if anybody could verify the picture I have in my head of how this works is correct I'd be grateful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <sstream>
#include <iostream>

using namespace std;

int main()
{
    stringstream str;

    str << '1';

    int x;
    str >> x;

    std::cout << "x + 1 is equal to : " << x + 1 << "\n";
    return 0;
}


Also, this I can't seem to reuse the same variable with a stringstream.

Line 19 below comes out to 3 instead of 4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <sstream>
#include <iostream>

using namespace std;

int main()
{
    stringstream str;

    str << '1';

    int x;
    str >> x;

    std::cout << "x + 1 is equal to : " << x + 1 << "\n";

    str << '2';
    str >> x;
    std::cout << "x + 2 is equal to : " << x + 2 << "\n"; //output comes out to 3 instead of 4.


    return 0;
}
Last edited on
Then some how (as if by magic) the stringstream converts the char to an integer.


The 'magic' here is the function istream::operator>>(int&) which is called when you write stream >> variable with any input or I/O stream on the left and the variable of type int on the right. That function (technically another C++ library function it calls) reads individual characters from the stream and builds an integer value out of them.

If you want to spend the rest of the day reading about the details, it is
http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt
http://en.cppreference.com/w/cpp/locale/num_get/get

(I feel that this site's http://www.cplusplus.com/reference/locale/num_get/get/ is less detailed)

Also, this I can't seem to reuse the same variable with a stringstream.

You happened to reach the end of the stream when executing str >> x; at line 13, so the next attempt to read from it, str >> x; on line 18, did nothing at all. You now have to call str.clear() before line 17, since while the stream is in the eof state, you can't write to it or read it anything at all.
Last edited on
I like to think of stringstreams as kind of like what cout and cin are, except instead of printing/reading from standard output/input (the screen), they work with std::strings.

So myistringstream >> myInt; converts to int using the same "magic" that cin >> myInt; does when you input an integer.

It follows that you can use the same data types with stringstreams as you can with cout or cin, keeping in mind that you're working with std::strings rather than standard output/input, of course.

For that last example code, it seems that the EOF flag is being set after the first str >> x; line (line 13), which causes the second str >> x; on line 18 to fail.
Putting str.clear(); after line 13 (which clears the error flags) gives you the desired output.

EDIT: Darn, got ninja'd. :)
Last edited on
Thanks for the info. I know it's completely unnecessary in many cases to know how something like this works.

I guess I get ocd sometimes about things like this and can't move on until I know how it works.


Thanks for the fix for the second part also. That works now.

Can I pretty much use any string function with a stringstream?
Last edited on
stringstreams don't have the string member functions, but you can use the .str() member function to return the string with the contents of the stringstream and then work with that string.
cool, that will be incredibly useful.
Topic archived. No new replies allowed.