This is all guesswork, but I'll try.
If you already know the size of the string that you need, you will most likely be copying data from somewhere else, e.g. from another string. In that case, you can call one of the constructors that accept char *
or const std::string &
to copy the data immediately.
Also, I can't see why using reserve
right after constructing a string is a bad thing. While it is implementation-defined, I would assume that it would make sense for this code:
std::string str;
str.reserve(100);
to allocate memory for a total of 100 elements, not 116 (as in "allocate 16 first, then free them and allocate 100 more"), thus having no performance impact over the non-existent reserve constructor.
Also, if you just want an empty string without the default allocation at all, you can presumably use std::string str(0, ' ');
which invalidates the "Using the fill constructor is a waste of time" point.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…