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.
- Why does
std::string::shrink_to_fit()
not work at expected? - How can I ensure that the string allocates the least amount of memory?