<p>I've tried several things already,</p>

<pre><code>std::stringstream m;
m.empty();
m.clear();
</code></pre>

<p>both of which don't work.</p>

[{'answer_id': 20732, 'author': 'CodingWithoutComments', 'author_id': 25, 'author_profile': 'https://Stackoverflow.com/users/25', 'pm_score': 5, 'selected': False, 'text': '<pre><code>m.str("");\n</code></pre>\n\n<p>seems to work.</p>\n'}, {'answer_id': 20792, 'author': 'Wilka', 'author_id': 1367, 'author_profile': 'https://Stackoverflow.com/users/1367', 'pm_score': 11, 'selected': True, 'text': '<p>For all the standard library types the member function <code>empty()</code> is a query, not a command, i.e. it means "are you empty?" not "please throw away your contents".</p>\n\n<p>The <code>clear()</code> member function is inherited from <code>ios</code> and is used to clear the error state of the stream, e.g. if a file stream has the error state set to <code>eofbit</code> (end-of-file), then calling <code>clear()</code> will set the error state back to <code>goodbit</code> (no error).</p>\n\n<p>For clearing the contents of a <code>stringstream</code>, using:</p>\n\n<pre><code>m.str("");\n</code></pre>\n\n<p>is correct, although using:</p>\n\n<pre><code>m.str(std::string());\n</code></pre>\n\n<p>is technically more efficient, because you avoid invoking the <code>std::string</code> constructor that takes <code>const char*</code>. But any compiler these days should be able to generate the same code in both cases - so I would just go with whatever is more readable.</p>\n'}, {'answer_id': 12309628, 'author': 'John', 'author_id': 1653342, 'author_profile': 'https://Stackoverflow.com/users/1653342', 'pm_score': -1, 'selected': False, 'text': '<p>These do not discard the data in the stringstream in gnu c++</p>\n\n<pre><code>    m.str("");\n    m.str() = "";\n    m.str(std::string());\n</code></pre>\n\n<p>The following does empty the stringstream for me:</p>\n\n<pre><code>    m.str().clear();\n</code></pre>\n'}, {'answer_id': 12843784, 'author': 'jerron', 'author_id': 1738731, 'author_profile': 'https://Stackoverflow.com/users/1738731', 'pm_score': 5, 'selected': False, 'text': '<p>This should be the most reliable way regardless of the compiler:</p>\n\n<pre><code>m=std::stringstream();\n</code></pre>\n'}, {'answer_id': 19439633, 'author': 'Francisco Cortes', 'author_id': 2406499, 'author_profile': 'https://Stackoverflow.com/users/2406499', 'pm_score': 4, 'selected': False, 'text': '<p>my 2 cents:</p>\n\n<p>this seemed to work for me in xcode and dev-c++, I had a program in the form of a menu that if executed iteratively as per the request of a user will fill up a stringstream variable which would work ok the first time the code would run but would not clear the stringstream the next time the user will run the same code. but the two lines of code below finally cleared up the stringstream variable everytime before filling up the string variable. (2 hours of trial and error and google searches), btw, using each line on their own would not do the trick.</p>\n\n<pre><code>//clear the stringstream variable\n\nsstm.str("");\nsstm.clear();\n\n//fill up the streamstream variable\nsstm &lt;&lt; "crap" &lt;&lt; "morecrap";\n</code></pre>\n'}, {'answer_id': 22668889, 'author': 'TimoK', 'author_id': 2757147, 'author_profile': 'https://Stackoverflow.com/users/2757147', 'pm_score': 4, 'selected': False, 'text': '<p>I am always scoping it:</p>\n\n<pre><code>{\n    std::stringstream ss;\n    ss &lt;&lt; "what";\n}\n\n{\n    std::stringstream ss;\n    ss &lt;&lt; "the";\n}\n\n{\n    std::stringstream ss;\n    ss &lt;&lt; "heck";\n}\n</code></pre>\n'}, {'answer_id': 23266418, 'author': 'Nikos Athanasiou', 'author_id': 2567683, 'author_profile': 'https://Stackoverflow.com/users/2567683', 'pm_score': 6, 'selected': False, 'text': '<p>You can <strong>clear the error state <strong>and</strong> empty the stringstream</strong> all in one line</p>\n<pre><code>std::stringstream().swap(m); // swap m with a default constructed stringstream\n</code></pre>\n<p>This effectively resets m to a default constructed state, meaning that <strong>it actually deletes the buffers allocated by the string stream and resets the error state</strong>. Here\'s an experimental proof:</p>\n<pre class="lang-cpp prettyprint-override"><code>int main ()\n{\n    std::string payload(16, \'x\');\n    \n    std::stringstream *ss = new std::stringstream; // Create a memory leak\n    (*ss) &lt;&lt; payload;                              // Leak more memory\n    \n    // Now choose a way to &quot;clear&quot; a string stream\n    //std::stringstream().swap(*ss); // Method 1\n    //ss-&gt;str(std::string());        // Method 2\n    \n    std::cout &lt;&lt; &quot;end&quot; &lt;&lt; std::endl;\n}\n</code></pre>\n<p><a href="http://coliru.stacked-crooked.com/a/937f6a279f4cc876" rel="noreferrer">Demo</a></p>\n<p>When the demo is compiled with address sanitizer, memory usage is revealed:</p>\n<pre><code>=================================================================\n==10415==ERROR: LeakSanitizer: detected memory leaks\n\nDirect leak of 392 byte(s) in 1 object(s) allocated from:\n    #0 0x510ae8 in operator new(unsigned long) (/tmp/1637178326.0089633/a.out+0x510ae8)\n    #1 0x514e80 in main (/tmp/1637178326.0089633/a.out+0x514e80)\n    #2 0x7f3079ffb82f in __libc_start_main /build/glibc-Cl5G7W/glibc-2.23/csu/../csu/libc-start.c:291\n\nIndirect leak of 513 byte(s) in 1 object(s) allocated from:\n    #0 0x510ae8 in operator new(unsigned long) (/tmp/1637178326.0089633/a.out+0x510ae8)\n    #1 0x7f307b03a25c in std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::reserve(unsigned long) (/usr/local/lib64/libstdc++.so.6+0x13725c)\n    #2 0x603000000010  (&lt;unknown module&gt;)\n\nSUMMARY: AddressSanitizer: 905 byte(s) leaked in 2 allocation(s).\n</code></pre>\n<p>Pretty steep if you ask me. To hold just 16bytes of payload, we spent 905 bytes ... string streams are no toy. Memory is allocated in two parts:</p>\n<ul>\n<li>The constructed string stream (392 bytes)</li>\n<li>The extra buffer needed for the payload (513 bytes). The extraneous size has to do with the allocation strategy chosen by the stream and for payloads &lt;= 8 bytes, blocks inside the initial object can be used.</li>\n</ul>\n<p>If you <a href="http://coliru.stacked-crooked.com/a/a9c231eb2783479b" rel="noreferrer">enable method 1</a> (the one shown in this answer) the extra 513 (payload) bytes are reclaimed, because the stream is <strong>actually cleared</strong>.</p>\n<p>If you <a href="http://coliru.stacked-crooked.com/a/de372b604e5b9b51" rel="noreferrer">enable method2</a> as suggested in the comments or other answers, you can see that all 905 bytes are in use by the time we exit.</p>\n<p>In terms of program semantics, one may only care that the stream &quot;appears&quot; and &quot;behaves&quot; as empty, similar to how a <code>vector::clear</code> may leave the capacity untouched but render the vector empty to the user (of course vector would spend just 16 bytes here). Given the memory allocation that string stream requires, I can imagine this approach being often faster. <strong>This answer\'s primary goal is to actually clear the string stream, given that memory consumption that comes with it is no joke</strong>. Depending on your use case (number of streams, data they hold, frequency of clearing) you may choose the best approach.</p>\n<p>Finally note that it\'s rarely useful to clear the stream without clearing the error state <strong>and all inherited state</strong>. The one liner in this answer does both.</p>\n'}, {'answer_id': 26307234, 'author': 'Alcino Dall Igna Junior', 'author_id': 4130754, 'author_profile': 'https://Stackoverflow.com/users/4130754', 'pm_score': -1, 'selected': False, 'text': "<p>It's a conceptual problem.</p>\n\n<p>Stringstream is a stream, so its iterators are forward, cannot return. In an output stringstream, you need a flush() to reinitialize it, as in any other output stream.</p>\n"}, {'answer_id': 69164375, 'author': "Kuba hasn't forgotten Monica", 'author_id': 1329652, 'author_profile': 'https://Stackoverflow.com/users/1329652', 'pm_score': 2, 'selected': False, 'text': "<p>There are many other answers that &quot;work&quot;, but they often do unnecessary copies or reallocate memory.</p>\n<ol>\n<li><p>Swapping streams means that you need to discard one of them, wasting the memory allocation. Same goes for assigning a default-constructed stream,</p>\n</li>\n<li><p>Assigning to the string in the string buffer (via <code>stringstream::str</code> or <code>stringbuf::str</code>) may lose the buffer already allocated by the string.</p>\n</li>\n</ol>\n<p>The canonical way to clear the string stream would be:</p>\n<pre><code>void clear(std::stringstream &amp;stream)\n{\n   if (stream.rdbuf()) stream.rdbuf()-&gt;pubseekpos(0);\n}\n</code></pre>\n<p>The canonical way to get the size of the data in the stream's buffer is:</p>\n<pre><code>std::size_t availSize() (const std::stringstream&amp; stream)\n{\n   if (stream.rdbuf())\n      return std::size_t(\n         stream.rdbuf()-&gt;pubseekoff(0, std::ios_base::cur, std::ios_base::out));\n   else\n      return 0;\n}\n</code></pre>\n<p>The canonical way to copy the data from the stream to some other preallocated buffer and then clear it would then be:</p>\n<pre><code>std::size_t readAndClear(std::stringstream &amp;stream, void* outBuf, std::size_t outSize)\n{\n   auto const copySize = std::min(availSize(stream), outSize);\n   if (!copySize) return 0; // takes care of null stream.rdbuf()\n\n   stream.rdbuf()-&gt;sgetn(outBuf, copySize);\n   stream.rdbuf()-&gt;pubseekpos(0); // clear the buffer\n\n   return copySize;\n}\n</code></pre>\n<hr />\n<p>I intend this to be a canonical answer. Language lawyers, feel free to pitch in.</p>\n"}]
['https://Stackoverflow.com/questions/20731', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/25/']