Reset string while maintaining capacity

I read a lot of questions here about how to completely clear the line (for example, reset the capacity, free the memory). My question, although it is the exact opposite; Is there any reliable way to discard a string (length) while still guaranteeing its capacity?

Example: reusing a timeline in a loop.

This will probably happen by default if I do something like

str.clear() str.reserve(256) 

in each iteration of the loop, at least when using Visual Studio according to the answer to this post: Concrete behavior of std :: string in visual studio?

But relying on the "probable" seems a bit risky.

+6
source share
1 answer

According to http://en.cppreference.com/w/cpp/string/basic_string/clear clear() does not free the internal buffer and does not preserve capacity .

+9
source

All Articles