The following code snippet:
string a = "abc";
cout << a.capacity();
a.erase(a.begin() + 1, a.end());
cout << a.capacity();
... Outputs:
33
Even if I remove some elements from the row, the capacity will remain the same. So my questions are:
Is any memory stored due to capacity? What if I don't explicitly reserve()'d?
If I use reserve()and do not use the full capacity, am I losing memory?
Should this extra memory (which I do not use) be allocated for something else, if necessary?
EDIT: Suppose y <
string a= "something";
a = "ab";
Now I know that athere will be no more than two characters. So is it wise to invoke reserve(2)so that memory is not wasted?
source
share