The STL has a class that is the string class, what is his memory layout and storage mechanism exactly? This is the creation of a good string can be seen, the figure is marked with a yellow box is the main area of the string we used to initialize string objects are stored in the _buf, _mysize and _myres, needless to say, is the above size and capacity value. When there is only one character as a string, it can be clearly seen, _mysize refers to the string of length_myres or not change, _myres the largest is 15? There is no change, then add a character can become 31, why has always been 2 of the second side-1, this is the string, the default to the string left a Terminator ' "" Space and when you give the initialization string more than 15, we store the position of the string changed, not in _ In the BUF, instead of the PTR, the next step is to take a look at how the copy is constructed, as you can see, when the pointer is not a shallow copy, but a deep copy; so is _buf the same? Yes, it's a
deep copy . So that is, in C + +, the String class uses a deep copy, and there is a default storage buffer, the size is 16 bytes, when the length of the stored string is exceeded, a pointer is used to store the string. And when the copy is constructed, the two parts will be deeply copied.
Probe into the string class in STL Library