title: How do you clear a std::ostringstream instance so it can be reused?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/624260/how-to-reuse-an-ostringstream">How to reuse an ostringstream?</a>  </p>
  </blockquote>
  
  
  
  <p>I have been using <code>std::ostringstream</code> to convert <code>float</code> and <code>int</code> values to strings but I cannot find anyway to reuse an instance. To illustrate what I mean here is the following along with the methods that I have tried to use to clear the stream</p>
  
  <pre><code> #include &lt;iostream&gt;
   #include &lt;sstream&gt;
   using namespace std;
  
   int main() {
     ostringstream stream;
     stream &lt;&lt; "Test";
     cout &lt;&lt; stream.str() &lt;&lt; endl;  
     stream.flush();                
     stream &lt;&lt; "----";
     cout &lt;&lt; stream.str() &lt;&lt; endl; 
     stream.clear();
     stream &lt;&lt; "****";
     cout &lt;&lt; stream.str() &lt;&lt; endl;
     return 0;
   }
  </code></pre>
  
  <p>generates output</p>
  
  <pre><code> Test
   Test----
   Test----****
  </code></pre>
  
  <p>This is giving me a problem as I am having to create many instances of <code>ostringstream</code> which is wasteful. Clearly <code>clear()</code> and <code>flush()</code> do not do what I need so is there a way to do this? I checked the documentation at <a href="http://www.cplusplus.com/reference/iostream/ostringstream/" rel="nofollow noreferrer">http://www.cplusplus.com/reference/iostream/ostringstream/</a> but there is nothing there that appears to do what I need. Is there a way to reset or clear the stream??</p>