how to reset stringstream object

realtekme picture realtekme · Jul 19, 2013 · Viewed 23.3k times · Source

I have a stringstream object and I am wondering how to reset it.

stringstream os;
for(int i = 0; i < 10; ++i){
        value = rand() % 100;
        os<<value;
        cout<<os.str()<<" "<<os<<endl;
        ntree->insert(os.str());
        //I want my os object to be reset here
    }

Answer

James Kanze picture James Kanze · Jul 19, 2013

If you want a new ostringstream object each time through the loop, the obvious solution is to declare a new one at the top of the loop. All of the ostream types contain a lot of state, and depending on context, it may be more or less difficult to reset all of the state.