c++ - std::string capacity size -
is capacity size of string multiple value of 15?
for example: in cases capacity 15
string s1 = "hello"; string s2 = "hi"; string s3 = "hey";
or random?
is capacity size of string multiple value of 15?
no; guarantee capacity of std::string
s.capacity() >= s.size()
.
a implementation grow capacity exponentially doubles in size each time reallocation of underlying array required. required std::vector
push_back
can have amortized constant time complexity, there no such requirement std::string
.
in addition, std::string
implementation can perform small string optimizations strings smaller number of characters stored in std::string
object itself, not in dynamically allocated array. useful because many strings short , dynamic allocation can expensive. small string optimization performed if number of bytes required store string smaller number of bytes required store pointers dynamically allocated buffer.
whether or not particular implementation performs small string optimizations, don't know.
Comments
Post a Comment