Vector containers use dynamic arrays to store and manage objects. Because the array is a random access data structure, elements in the random access vector can be used. It is time-consuming to insert an element in the middle or at the beginning of an array, especially when the array is very large. However, inserting elements at the end of the array is fast. The Class Name of the implementation vector container is vector (the container is a class template ). The header file name containing the Vector class is vector. Therefore, to use vector containers in a program, you must include the following statement in the program: # Include <vector> In addition, when defining a vector type object, you must specify the object type, because the Vector class is a class template. For example, the statement: Vector <int> intlist; Declare intlist as a vector container object whose element type is int. Similarly, the statement: Vector <string> stringlist; declare stringlist as a vector container object whose element type is string. Declare a vector object The vector class contains multiple constructors, including Default constructors. Therefore, you can declare and initialize vector containers in multiple ways. Table 1 describes how to declare and initialize a vector container of the specified type. Table 1 declaration and initial vector container Methods
Statement
|
Function |
Vector <elementtype> veclist;
|
Create an empty vector veclist without any elements (use the default constructor) |
Vector <elementtype> veclist (otherveclist) |
Create a vector veclist and use the element in the vector otherveclist to initialize the vector. Vector veclist and vector otherveclist are of the same type |
Vector <elementtype> veclist (size );
|
Create a vector veclist with a size and use the default constructor to initialize the vector. |
Vector <elementtype> veclist (n, ELEM );
|
Create a n-sized vector veclist. All n elements in the vector are initialized to ELEM. |
Vector <elementtype> veclist (begin, end );
|
Create a vector veclist and initialize the elements in the vector (begin, end. That is, all elements from begin to end-1 |
After introducing how to declare vector sequence containers, let's start to discuss how to operate the data in vector containers. First, you must know the following basic operations: Element insertion Element Deletion Traverse elements in a vector container Assume that veclist is a vector container. Table 2 provides operations for inserting and deleting elements in the veclist. These operations are member functions of the Vector class. Table 2 also describes how to use these operations. Table 2 operations on vector containers
Statement |
Function |
Veclist. Clear () |
Delete all elements from the container |
Veclist. Erase (position)
|
Deletes an element at a position specified by position. |
Veclist. Erase (beg, end)
|
Delete all elements from beg to end-1 |
Veclist. insert (Position, ELEM)
|
Insert a copy of ELEM to the position specified by position and return the position of the new element. |
Veclist. inser (Position, N, ELEM) |
Insert n copies of ELEM to the position specified by position. |
Veclist. insert (Position, beg, end) |
Insert copies of all elements from beg to end-1 to the position specified by position in the veclist. |
Veclist. push_back (ELEM)
|
Insert a copy of ELEM to the end of the list |
Veclist. pop_back ()
|
Delete last element |
Veclist. Resize (Num)
|
Change the number of elements to num. If size () is increased, the default constructor is responsible for creating these new elements. |
Veclist. Resize (Num, ELEM) |
Change the number of elements to num. If size () increases, the default constructor initializes these new elements to ELEM |
Declare the iterator in the vector container The vector class contains a typedef iterator, which is a public member. The iterator can be used to declare the iterator in the vector container. For example, the statement: Vector <int>: iterator intveciter; declare intveciter as an int Type Vector container iterator. Because iterator is a typedef defined in the vector class, you must use the container name (vector), container element type, and scope operator to use iterator. Expression: ++ Intveciter Add intveciter 1 to point it to the next element in the container. Expression: * intveciter Returns the element at the current iterator position. Note that the operations on the iterator are the same as those on the pointer. * Is used as a single object operator and is called a reference operator. The following describes how to use an iterator to manipulate data in a vector container. Assume the following statement is available: Vector <int> intlist; Vector <int>: iterator intveciter; The statement in the first line declares intlist as a vector container whose elements are int type. The statement in the second line declares intveciter as an iterator for vector containers whose elements are int type. Container and function begin and end All containers contain the member functions begin and end. The begin function returns the position of the first element in the container. The end function returns the position of the last element in the container. Both functions have no parameters. Execute the following statement: Intveciter = intlist. Begin (); The iterator intveciter points to the first element in the intlist container. The following for loop outputs all elements in the intlist to the standard output device: For (intveciter = intlist. Begin (); intveciter! = Intlist. End (); Cout <* intveclist <""; You can directly access the elements in the vector container through the operations given in table 3. Table 3 operations on elements in an access Vector container
Expression
|
Function |
Veclist. At (INDEX) |
Returns the element at the position specified by the index. |
Veclist [Index]
|
Returns the element at the position specified by the index. |
Veclist. Front ()
|
Returns the first element (do not check whether the container is empty) |
Veclist. Back ()
|
Return the last element (do not check whether the container is empty) |
Table 3 Description: Elements in a vector can be processed in an array (Note: In C ++, the array subscript starts from 0 ., The position of the first element in the vector container is also 0 ). The emblem class also contains: A member function that returns the number of current elements in the container, and a member function that returns the maximum number of elements that can be inserted into the container. Table 4 describes some of the operations (assuming veccont is a vector container ). Table 4 operations for calculating the vector container size
Expression
|
Function |
Veccont. Capacity () |
Returns the maximum number of elements that can be inserted into the veccont of the container without allocating space. |
Veccont. Empty ()
|
If the container veccont is null, true is returned. Otherwise, false is returned. |
Veccont. Size ()
|
Returns the current number of veccont containers. |
Veccont. max_size () |
Returns the maximum number of elements that can be inserted into the veccont of the container. |
Below is a sample program for further understanding of the usage of these functions: # Include <iostream> # Include <vector> Using namespace STD; Int main () { Vector <int> intlist; Int I; Intlist. push_back (13 ); Intlist. push_back (75 ); Intlist. push_back (28 ); Intlist. push_back (35 ); Cout <"Line 1: List elements :"; For (I = 0; I <4; I ++) cout <intlist [I] <""; For (I = 0; I <4; I ++) intlist [I] * = 2; Cout <"Line 2: List elements :"; For (I = 0; I <4; I ++) cout <intlist [I] <""; Cout <Endl; Vector <int>: iterator listit; Cout <"line 30: list elements :"; For (listit = intlist. Begin (); listit! = Intlist. End (); ++ listit) cout <* lintit <""; Cout <Endl; Listit = intlist. Begin (); ++ Listit; ++ Listit; Intlist. insert (listit, 88 ); Cout <"line 4: List elements :"; For (listit = intlist. Begin (); listit! = Intlist. End (); ++ listit) cout <* listit <""; Cout <Endl; Return 0; } Running program output: Line 1: List elements: 13 75 28 35 Line 2: List elements: 26 150 56 70 Line 3: List elements: 26 150 56 70 Line 4: List elements: 26 150 88 56 70 Note: when an object is a constant, you can only call the member functions declared with the const modifier in the class. The back () and subscript operators [] of the member functions have an independent implementation and are declared as Const. Therefore, these functions can be called by constant objects. For example:
Template <typename T> Void writevectorends (const vector <t> & V ){ If (V. Size ()> 0) cout <V [0] <"" <v. Back () <Endl; } |