Vector of C ++ sequential containers, vector of sequential containers

Source: Internet
Author: User

Vector of C ++ sequential containers, vector of sequential containers

 

 

What is container

Container, as its name implies, is a place for storing things. C ++ containers store certain data structures for data search or sorting or other special purposes. As we all know, common data structures are similar to arrays.Array, Linked listList, TreeTreeStackStack, QueueQueue, HashHash Table, SetSet, Ing tableMapAnd so on. Containers can accommodate these data structures. These data structures are divided into two types: sequential and associative, so containers are also divided into sequential and Associative containers.

   

(Figure from STL source code analysis)

 

Vector Overview

  

1. vector is a sequence container provided by STL.  

The elements in a sequence container are ordered, but not necessarily ordered. That is, the element sets are arranged in a linear relationship, but not necessarily ordered. C ++ itself carries a sequential container array. STL then provides other sequential containers: vector, list, deque, stack, queue, priority-queue, etc.

2. The bottom layer of the vector is a dynamic array.

The data arrangement and operation method of vector are very similar to the array of C ++. The only difference between them is the flexibility of space usage. Array is a static array, which has the biggest drawback: only a certain size of storage space can be allocated each time. When a new element is inserted, we need to go through "finding a larger memory space"-> "copying data to a new space"-> "destroying the old space" trilogy. For array, this type of space task is applied to users who use it. Users must grasp the number of data and try to allocate reasonable space to the data during the first allocation (this is sometimes difficult ), to prevent the cost of the "trilogy", and data overflow is also a problem that users need to pay attention to in static arrays.

However, vector users do not need to handle space usage issues. Vector is a dynamic space. With the insertion of new elements, when the old storage space is insufficient, the internal mechanism of the vector will expand the space to accommodate new elements. Of course, in most cases (almost) it cannot escape the "trilogy", but does not need to be processed by the user, and the vector processing is safer and more efficient. The key to the implementation of vector lies in the control of its size and the efficiency of data Movement During reconfiguration.

3. vector iterator

  For arrays in C language, we can use common pointers to perform various operations on arrays. Vector maintains a continuous linear space, which is the same as array. Therefore, no matter what the element type is, normal pointers can be used as vector iterators to meet all necessary conditions. The iterator operations required by vector, including operator *, operator->, operator ++, operator --, operator + =, operator-=, and so on.

Therefore,Normal pointers can meet the needs of vector for iterators.Therefore, vector provides Random Access Iterators.

4. Data Structure of vector

As mentioned above, the bottom layer of a 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. These two iterators indicate the used range of the continuous linear space, and the end of the continuous linear space according to the end_of_storage iterator. Here, begin and finish comply with the STL "open before and close" standard.

Based on these three iterators, you can perform many operations. Including the marker, size, capacity, empty container judgment, [] Operator, frontend element value, and end element value.

It is worth noting that the container size and capacity are different. The container size is equal to the container size only when the container is fully loaded. In the figure above, the size isUsedAnd the capacity isUsed + not usedThe length of the bucket. We can also see from their implementation code:

  

size_type size() const{      return size_type( end() - begin() ) ;}size_type capacity () const{     return size_type( end_of_storage - begin() );}

5. Memory Allocation Policy of vector

The implementers of the standard library use the memory allocation policy to store elements continuously at minimal cost. To enable the vector container to implement fast memory allocation, the actual allocated capacity is much larger than the current space required. The vector container reserves these additional storage areas for storing new elements, therefore, you do not have to allocate memory for each new element. When an element is added to the container, the backup space is used up (beyond capacity). When the element is added, the memory management mechanism of the vector expands to two times, if twice the capacity is still insufficient, it will be expanded to a large enough capacity. Capacity Expansion must go through the huge project of "reconfiguration, element movement, and release of the original space. According to the vector source code provided in STL source code analysis, the memory configuration principle of vector is as follows:

 If the original vector size is 0, 1 is configured, that is, the size of an element.

If the original size is not 0, double the original size.

Of course,Each implementation of vector can freely select its own memory allocation policy. The memory allocation policy depends on its implementation method. Different databases adopt different allocation policies.

Note that when using the vector iterator, always pay attention to whether the vector has been resized. Once the expansion causes space reconfiguration, it points to allIteratorAllInvalid.

 

The usage of various vector interfaces will not be described here. A new understanding of vector will update blog posts in a timely manner.

  

 

  

Related Article

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.