Jan 18, 2008

stringstream bug?

What the hell happened to std::stringstream. I have just noticed this while helping my colleague to debug his code.

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
stringstream ss("Test");
cout << "#1: " << ss.str() << endl;

ss << "another string";
cout << "#2: " << ss.str() << endl;
return 0;
}

Output:

#1: Test
#2: another string

stringstream has eaten the initial string, which was provided as a parameter to the constructor.

OS: Fedora 8
Compiler: g++ (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)

this one is nice:

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
ostringstream ss("Test");
cout << "#1: " << ss.str() << endl;

ss << "--";
cout << "#2: " << ss.str() << endl;
return 0;
}

Output:

#1: Test
#2: --st

No comments: