Vector, dynamic array, similar to array, can dynamically allocate memory. Memory Implementation
A string similar to a contiguous space. There is the concept of using space and currently available space. Start: Beginning of used space finish: End of used space end_of_storage: current free space End
When there is not enough space, new space is automatically allocated and the old address content is copied to the new address. The new allocation space size growth rule is:
Linux is twice times the previous one, and Windows is 1.5 times times the previous one.
One point about space allocation Note: When it comes to new memory allocations, pay special attention to the old iterators being invalidated. Operation
According to Start,finish, End_of_storage can easily get the operation Begin:return start, return the iterator End:return finish, and return the iterator size: Returns the current occupied space size capcity: Returns the usable space capacity empty: Judge Non-null Front: first element back: last element operator[]: * (Begin () +n) at: similar to opeator[]
Other actions: (Note: The following iter,first,last are all iterators, begin/end for the beginning end of the above description iterator) Push_back: End insert element pop_back: End popup element Erase (ITER): Delete a location element erase (first,last): Delete all elements in [first,last] clear: That is, erase (start,finish) Insert (iter,size,n): Insert the size element before ITER, the element content is N, Returns the new iterator insert (ITER,N) for the first element inserted: Insert element n before ITER
With regard to the insert operation, it is noted that the ITER and subsequent iterators fail after insertion (because the vector will fail if it inserts an element in front of an iterator). The standard does not specify what should be done with a failed iterator. If you want to use it, you can use the new iterator to accept the return value.
Vector<int> v;
V.push_back (1);
V.push_back (2);
Vector<int>::iterator iter = V.begin ();
V.insert (ITER, 3);
V.insert (ITER, 4); Error, after the insert operation ITER and its subsequent iterators can not be used
iter = V.insert (ITER, 3);
V.insert (ITER, 4); Correct, use ITER to receive a new iterator
Resize: Adjusts the length of the container so that it can hold n elements. If n is less than the current size of the container, remove the extra elements
Reserver: A container with a pre-allocated space of N that is known to be inserted, to avoid continuous copying of new space operations caused by constant push_back, and to allocate container size in advance
Swap: As the name suggests, swap two containers or elements. One of the ingenious uses is often used to clear the space occupied by the vector.
We know that the vector performs a clear operation, which is equivalent to executing erase (start,finish), and viewing the source code is only a destructor of all the elements inside, and does not release the space occupied by the vector. Use the following example to verify.
Vector<int> v;
for (int i = 0; i < ++i)
{
v.push_back (i);
}
cout << v.capacity () << Endl;
V.clear ();
cout << v.capacity () << Endl; See the results, consistent with the above output.
Here you can use swap to release the space the vector occupies. Swap technique realizes memory release thought: Vector () creates a temporary vector object using the vector's default constructor, and then invokes the swap member on the temporary object, and the space occupied by the object Myvector after the swap call equals the size of a default constructed object. The temporary object has the size of the original object V, and the temporary object is then refactored so that the space it occupies is freed.
Vector<int> v;
for (int i = 0; i < ++i)
{
v.push_back (i);
}
vector<int> tmp;
Tmp.swap (v);
Vector<int> (). Swap (v); Shorthand