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