Vector of C + + sequential containers

Source: Internet
Author: User

What is a container

A container, as the name implies, is a place for placing things. C + + containers allow for a data structure that facilitates the search or sequencing of the information or other special purposes. As is known to all, the commonly used data structures are: Arrays array , listof lists, tree trees, stack stacks, queue queues, hash list hash table, set set, map table map, and so on. Containers are those that hold these data structures. The data structure is divided into two types: sequential and associative, so the container is also divided into sequential container and associative container.

   

(Figure from "STL Source Code Analysis")

Vector overview

  

1. Vector is a sequential container provided by STL  

The so-called sequence container, where the elements are ordered, but not necessarily orderly, that is, the element set is linear relationship arrangement, but not necessarily orderly. C + + itself has a sequence container array,stl and then provides other sequential containers: Vector,list,deque,stack,queue,priority-queue and so on.

2. Vector bottom is a dynamic array

  Vector data is arranged and operated in a very similar way to the array of C + +, where the only difference is the flexibility in the use of space. Array is a static array, with the biggest disadvantage of static arrays: only a certain amount of storage space can be allocated at a time, when a new element is inserted, going through the "find a larger memory space," "Copy the data to the new space", "destroy the Old Space" trilogy, and for Array, This space task pressure on the users who use it, the user must grasp the number of data, as far as possible in the first allocation of the data to allocate a reasonable space (this is sometimes difficult to do) to prevent the "trilogy" brought about the cost, and data overflow is also a static array users need to pay attention to the problem.

Vector users do not need to personally deal with the problem of space use. Vector is a dynamic space, with the insertion of new elements, old storage space is not enough, the vector internal mechanism will expand its own space to accommodate the new elements, of course, this space expansion in most cases (almost) can not escape the "trilogy", but do not need the user to handle, And the vector is handled more safely and efficiently. The key to vector implementation is the control of its size and the efficiency of data movement during reconfiguration.

3. Vector iterator

  For the C language array, we can use the normal pointer to perform various operations on the arrays. The vector maintains a continuous linear space, as with array arrays, so the normal pointer can be used as the vector iterator to satisfy all the necessary conditions, regardless of the element type. The iterator operations required by vectors, including Operator*,operator->,operator++,operator--, operator+=,operator-=, are common pointers.

Therefore, the normal pointer can meet the needs of the vector to the iterator. So, Vector provides the random Access iterators.

4. Vector data structure

As mentioned above, the underlying vector is a continuous linear space. It uses two iterators: Begin and finish the position of the first element in the continuous linear space and the first position beyond the end, both of which mark the used range of the continuous linear space and the end of the entire continuous linear space with the End_of_storage iterator standard. Here the begin and finish conform to the STL's "Front open and closed" standard.

  

With these three iterators, you can do a lot of things. This includes providing the end-to-end indication, size, capacity, empty container judgment, [] operator, the front-most element value, the last-port element value, and so on.

It is worth noting that the container size and capacity are not the same concept. The size is equal to the container only when the container is full. In the above figure, size is the length of storage space used , and the capacity is used + unused storage space. It can also be seen from their implementation code:

  

Const {      return size_type (end ()-const{     return size_type ( End_of_storage- begin ());}

5. Vector's memory allocation policy

  The implementation of the standard library uses such a memory allocation strategy: Elements are stored continuously at minimal cost. In order for the vector container to achieve fast memory allocation, its actual allocation capacity is more than the current space required, the vector container reserved These additional storage for the addition of new elements, so do not have to do a memory allocation for each new element. When adding elements to the container causes the spare space to be exhausted (exceeding the capacity capacity), the vector's memory management mechanism expands to twice times when the element is added, and if twice times the capacity is insufficient, it expands to a sufficiently large capacity. Capacity expansion must undergo a vast project of "reconfiguration, element movement, release of the original Space". According to the vector source code provided in the STL source analysis, Vector's memory configuration principle is:

 If the vector original size is 0, then configure 1, which is the size of an element.

If the original size is not 0, the configuration is twice times the original size.

of course,each vector implementation is free to choose its own memory allocation strategy, how much memory is allocated depends on how it is implemented, and different libraries adopt different allocation strategies.

It is important to note that vector iterators are used to keep an eye out for the expansion of vectors, and once the expansion causes space reconfiguration, all iterators pointing to the original vector will be invalidated .

How to use the various interfaces of vectors here is no longer a repeat. New knowledge of vectors will update the blog post in a timely manner.

  

  

Vector of C + + sequential containers

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.