Probe into the string class in STL Library

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.