I've read a lot of questions here on SO about how to clear a string entirely (i.e. resetting the capacity, freeing memory). My question though is the exact opposite; is there any reliable way of resetting a string (the length) while being guaranteed that its capacity is kept intact?
Example: reusing a temporary string in a loop.
Likely this will happen by default if I do something like
str.clear()
str.reserve(256)
in each loop iteration, at least when using Visual Studio according to the answer to this post: Specific behaviour of std::string on visual studio?
But relying on "likely" seems a bit risky.
According to http://en.cppreference.com/w/cpp/string/basic_string/clear clear()
doesn't free internal buffer and keeps capacity
intact.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With