Since this is completely implementation details, you cannot determine this with 100%
accuracy.
However, as you said, you want to put some code to statistics the memory usage in your program, then you can do that with some accuracy.
I believe, for std::string
, the size of memory taken by string objects, will be almost equal to:
size_t statisticalSizeStr = sizeof(string)+ s.capacity() * sizeof(char);
And similarly, for std::vector
size_t statisticalSizeVec = sizeof(std::vector<T>)+ ....;
You can model your statistical estimation on such informations. For vector, you can also consider the size of T
in the same way which will fill the ....
in the above equation. For example, if T
is std::string
, then:
size_t vecSize = sizeof(std::vector<std::string>);
size_t statisticalSizeVec = vecSize + v.capacity() * statisticalSizeStr;
And if T
is int
, then
size_t statisticalSizeVec=sizeof(std::vector<int>)+v.capacity()*sizeof(int);
I hope, such analysis would help you to compute the size with as much accuracy as possible.