Summary of realization principle of STL source code anatomy--vector

Source: Internet
Author: User



vector data is arranged and manipulated in a very similar way to array. The only difference between the two is the flexibility of the use of space. Array is static space, once configured can not be changed; to change a big (or small) a little house, can, all trivial must be by the client itself: first configure a new space, and then the elements from the site one by one moved to the site, and then the original space released back to the system. Vector is a dynamic space, with the addition of elements, its internal mechanism will expand its own space to accommodate the new elements. Therefore, the use of vectors for the rational use of memory and the flexibility of the use of a great help, we do not have to be afraid of the lack of space and the beginning of a large array, we can safely use the array, how much to eat.

The key to vector implementation is the control of size and the efficiency of data movement when reconfiguration. Once the old space of the vector is fully loaded, if the client adds a new element to it, the inside of the vector is merely a space for an element, which is unwise. Because the so-called expansion of space (no matter how large), as mentioned earlier, is "the allocation of new space/data movement/release of the old space ," The Big project, the time cost is high, should be added to some kind of precautionary consideration.

In addition, because the vector maintains a continuous linear space, the vector supports random access .

Note: Vector dynamically increases the size, not after the original space after the continuous new space (because there is no guarantee of space after the original space), but instead of twice times the original size of a larger space, and then copy the original content, and then began to construct new elements after the original content, and release the original space. Therefore, any action on the vector, once the space is reconfigured, is invalidated by all iterators pointing to the original vector . This is a mistake that programmers are prone to make and need to be careful.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Summary of realization principle of STL source code anatomy--vector

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: info-contact@alibabacloud.com 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.