Flushing stringstream

I'm trying to flush a stringstream variable:

1
2
3
4
stringstream x;
x << "abc";
x << flush << "def";
cout << x << endl;


I expect to see "def" but what I seem to be getting is "abcdef". Is this correct and how can I get the desired value.

thanks
flushing has no meaning for stringstream, as there is no external device that it sends data to.
Based on your use case, you want to empty the stringstream, that's done by calling str("")

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    stringstream x;
    x << "abc";
    x.str("");
    x << "def";
//    cout << x << endl; // this will not print the strnig either way
    cout << x.str() << '\n';   // it's either .str()
    cout << x.rdbuf()  << '\n'; // or rdbuf()
}
Topic archived. No new replies allowed.