Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the buffer of a streamstring?

I have a streamstring in two loops and it is burning my RAM. So how to clear properly the buffer of a steamstring? It is like that to simplify :

stringstream ss (stringstream::in | stringstream::out);

for()
{
    for()
    {
        val = 2;
        ss << 2;
        mystring = ss.str();
        // my stuff
    }
    // Clear the buffer here
}

It wrote 2 then 22 then 222... I tried .clear() or .flush() but it is not that. So how I do this?

like image 698
bdelmas Avatar asked Dec 01 '22 02:12

bdelmas


1 Answers

The obvious solution is to use a new stringstream each time, e.g.:

for (...) {
    std::stringstream ss;
    for (...) {
        //  ...
    }
}

This is the way stringstream was designed to be used. (Also: do you really want a stringstream, or just an ostringstream?)

like image 125
James Kanze Avatar answered Dec 06 '22 09:12

James Kanze