1. Container element type
Most data types in C + + can be the element type of a container. The container element type must satisfy two conditions: assignment and copy operations are supported.
So no element is a container of the reference type, and neither the Io object nor the auto_ptr is the element type of the container.
2, vector container self-growth
The elements stored in the vector container are stored continuously in memory. If there is no space in the container to accommodate the new element, because the element must be stored continuously for index access, the new element cannot be stored anywhere in memory, so the vector must reallocate space for the original element and the newly added element: the elements stored in the old container are copied into the new container. , insert a new element, and finally undo the old storage space. In order for the vector container to achieve fast memory allocation, it actually allocates more capacity than is currently required, and the vector container reserves these spaces for storing new elements.
When you add an element to the list container, you only need to create a new element and then connect that element to the list that already exists, without reallocating the storage space or duplicating any existing elements.
3, Vector, deque, list three kinds of container characteristics
Vector: Supports fast random access, efficiently adding deleted data to the tail of the vector container
Deque: Supports fast random access, efficiently adding deleted data to the Deque container head and tail
List: Sequential access is supported, but inserting delete elements at any location is fast
4, vector container basic operation
- #include " stdafx.h "  
- #include < span class= "tag" style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">< string > &NBSP;&NBSP;
- #include < span class= "tag" style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">< iostream > &NBSP;&NBSP;
- #include < span class= "tag" style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">< vector > &NBSP;&NBSP;
- using namespace Std;
- int main (int argc, char* argv[])
- {
- //vector defines and initializes the
- vector int > Span style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">&NBSP;V1;&NBSP;&NBSP;
- for (int i = 0 ;i < 10 ;i++)
- V1.push_back (i+1);
- //uses iterators to traverse vector
- vector int > Span style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">::ITERATOR&NBSP; iter = v1 .begin ();
- for (; Iter!=v1.end (); iter++)
- {
- cout< < *iter < <
- }
- cout< < Span class= "Tag-name" style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">endl ;&NBSP;&NBSP;
- return 0;
- }
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
C + + sequential container vector, deque, list