Before I begin, I need to state that my application uses lots of strings, which are on average quite small, and which do not change once created.
In Visual Studio 2010, I noticed that the capacity of std::string is at least 30. Even if I write std::string str = "test";
, the capacity of str is 30. The function str.shrink_to_fit()
does nothing about this although a function with the same name exists for std::vector and works as expected, namely decreasing the capacity so that capacity == size.
std::string::shrink_to_fit()
not work at expected?std::string
implementation most likely uses some form of the short string optimization resulting in a fixed size for smaller strings and no effect for shrink_to_fit
. Note that shrink_to_fit
is non-binding for the implementation, so this is actually conforming.vector<char>
to get more precise memory management, but would loose some of the additional functionality of std::string
. You could also write your own string
wrapper which uses a vector
internally.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