C ++
The built-in array supports the container mechanism, but it does not support the abstract semantics of the container. To solve this problem, we need to implement such a class. In standard C ++, container vector is used. Container Vector
It is also a class template.
Use the header file # include <vector> for the vector type of the standard library. Vector
Is a class template. Vector <int> is a data type. Vector storage space is continuous, list
Not continuously stored
.
I. Definition and initialization
Vector <typename> V1; // V1 is empty by default. Therefore, the following assignment is incorrect. V1 [0] = 5;
Vector <typename> V2 (V1); or v2 = V1; or vector <typename>
V2 (v1.begin (),
V1.end (); // V2 is a copy of V1. If v1.size ()> v2.size () is assigned, v2.size () is expanded
V1.size ().
Vector <typename> V3 (n, I); // V3 contains N typename elements whose values are I.
Vector <typename> V4 (n); // V4 contains n elements whose values are 0.
Int A [4] = {0, 1, 2, 3}; vector <int>
V5 (A, A + 5); // The V5 size is 5, and V5 is initialized to 5 values of. The next Pointer Points to the next position of the last element to be copied.
Vector <int> V6 (V5); // V6 is a copy of V5.
Vector <type> identifier (maximum capacity, all initial values );
Ii. Value Initialization
1> if no element initialization formula is specified, the standard library provides an initialization value for value initialization.
2> If the saved expression contains elements of the class type of the constructor, the standard library uses this type of constructor for initialization.
3> If the saved expression does not have the class elements of the constructor, the Standard Library generates an object with an initial value and uses this object for value initialization.
Iii. Most important operations of vector objects
1. V. push_back (t)
Add a data value t at the end of the container, and the container size increases.
In addition, the list has the push_front () function. When inserted at the front end, the element subscript increases sequentially.
2. V. Size ()
Returns the number of data in the container. Size returns the size_type value defined by the corresponding Vector class. V. Resize (2 * v. Size)
Or
V. Resize (2 * v. Size, 99)
Double the capacity of V (and initialize the value of the new element to 99)
3. V. Empty ()
Determines whether the vector is empty.
4. V [N]
Returns the element whose position is N in v.
5. V. insert
(Pointer, number, content)
Insert the content of number content at the position pointed to by pointer in v.
And v. insert (pointer,
Content), V. insert (pointer, a [2], a [4]) insert three elements from a [2] to a [4.
6. V. pop_back ()
The last element of the container is deleted. This element is not returned.
7. V. Erase (pointer1, pointer2)
Delete the elements from pointer1 to pointer2 (including the elements referred to by pointer1.
After deleting an element in the vector, all elements after this position need to move forward to a position, although the current iterator position does not automatically add 1,
However, due to the sequential forward of subsequent elements, it is equivalent to automatically pointing to the next position of the iterator.
8. V1 = V2: Determine whether V1 and V2 are equal.
9 .! =, <, <=,>,> = Keep the meanings of these operators.
10. Vector <typename>: iterator
P = v1.begin (); the initial value of P points to the first element of V1. * P indicates the value of the element to which it points.
For const
Vector <typename> can only be accessed using a pointer of the vector <typename>: const_iterator type.
11. P = v1.end ();
P points to the next position of the last element of V1.
12. V. Clear ()
Delete all elements in the container. 12. V. Clear () delete all elements in the container.