Is the capacity size of string always a multiple value of 15?
for example: In all cases the capacity is 15
string s1 = "Hello";
string s2 = "Hi";
string s3 = "Hey";
or is it random?
Is the capacity size of string always a multiple value of 15?
for example: In all cases the capacity is 15
string s1 = "Hello";
string s2 = "Hi";
string s3 = "Hey";
or is it random?
Is the capacity size of string always a multiple value of 15?
No; the only guarantee about the capacity of a std::string is that s.capacity() >= s.size().
A good implementation will probably grow the capacity exponentially so that it doubles in size each time a reallocation of the underlying array is required. This is required for std::vector so that push_back can have amortized constant time complexity, but there is no such requirement for std::string.
In addition, a std::string implementation can perform small string optimizations where strings smaller than some number of characters are stored in the std::string object itself, not in a dynamically allocated array. This is useful because many strings are short and dynamic allocation can be expensive. Usually a small string optimization is performed if the number of bytes required to store the string is smaller than the number of bytes required to store the pointers into a dynamically allocated buffer.
Whether or not your particular implementation performs small string optimizations, I don't know.
<string> header and take a look. Is there a particular reason that you need to know or are you just curious?<string> header file. I need to append a character \n at the end of my std::string but I want to be sure that it doesn't need reallocation Or otherwise I can allocate memory on heap without needing std::string.std::string has a reserve member function that behaves just as std::vector::resize does. If you need to append another character but size() == capacity(), there's really no way around a reallocation.