There are several constructors for std::string
. I was looking for a way to avoid reallocation and I'm surprised that there is a fill constructor but no "reserve" constructor.
std::string (size_t n, char c);
but no
std::string (size_t n);
So do I have to call reserve()
after it already allocated the default (16 bytes in my case), just to immediately reallocate it?
Is there a reason why there is no such constructor to reserve space directly when the object is created, instead of having to do it manually? Or am I missing something and there is some way to do this?
Using the fill constructor is a waste of time, because it will loop through the memory just to get overwritten, and also cause a wrong size, because s.length()
reports N
instead of 0
.