The vector container type vector container is a template class that can store any type of objects (but must be the same class object ). The vector object can efficiently add elements at runtime, and the elements in the vector are continuously stored. Vector constructor prototype: Template <typename T> explicit vector (); // default constructor. The vector object is empty. Explicit vector (size_type N, const T & V = T (); // create a vector object vector (const vector & X) with N elements; vector (const_iterator first, const_iterator last); note: all objects in the vector container memory are initialized. If the initial value of the stored object is not specified, the built-in type will be initialized with 0, and the default constructor will be called for class type initialization (if there are other constructor and no default constructor, in this case, the initial element values must be provided before they can be placed in the container ). Example: vector <string> V1; // creates an empty container. Its object type is string-type vector <string> V2 (10 ); // create a vector <string> V3 (5, "hello") Container with 10 string objects with an initial value (that is, an empty string "); // create a vector <string> V4 (v3.begin (), v3.end () container for five string objects with the value "hello ()); // V4 is the same as V3 container (completely copied) vector operation (the following functions are all member functions) bool empty () const; // if the container is empty, returns true; otherwise, returns falsesize_type max_size () const; // returns the maximum number of elements that the container can accommodate size_type size () const; // returns the number of elements in the container size_type capacity () const; // Number of elements that the container can store, including: Capacity ()> = size () void reserve (size_type N); // ensure capacity ()> = nvoid resize (size_type N, t x = T (); // make sure that the returned result contains: size () = N; if the previous size () <n, then fill in the value of element x. Reference Front (); // return the reference of the first element in the container (the container must be not empty) const_reference Front () const; reference back (); // return the reference of the last element in the container (the container must be not empty) const_reference back () const; reference operator [] (size_type POS ); // return the reference of an element whose subscript is pos (the subscript starts from 0. If the subscript is incorrect, it is undefined. Const_reference operator [] (size_type POS) const; reference at (size_type POS); // return the reference of the element whose subscript is pos. If the subscript is incorrect, throw an exception out_of_rangeconst_reference at (size_type POS) const; void push_back (const T & X); // Add an element void pop_back () to the end of the container (); // The last element in the popup container (the container must be not empty) // Note: The following insert and delete operations will move the elements (to maintain the nature of continuous storage ), therefore, the previous iterator may fail iterator insert (iterator it, const T & X = T (); // insert an element before the insert point element (or insert an element at the insert point) void insert (iterator it, size_t Ype N, const T & X); // note that the iterator may no longer be valid (space may be re-allocated) void insert (iterator it, const_iterator first, const_iterator last ); iterator erase (iterator it); // Delete the specified Element and return the position of the last element after the deletion (if there is no element, return end () iterator erase (iterator first, iterator last); // Note: After an element is deleted, the iterator corresponding to the element after the deleted vertex is no longer valid. Void clear () const; // clear the container, which is equivalent to calling erase (begin (), end () void assign (size_type N, const T & X = T ()); // assign values. Replace void assign (const_iterator first, const_iterator last); const_iterator begin () const; // Iteration Sequence iterator begin (); const_iterator end () const; iterator end (); const_reverse_iterator rbegin () const; reverse_iterator rbegin (); rend () const; reverse_iterator rend (); Comparison of vector objects (non-member functions) there are six comparison operators for comparison of vector objects: Operator =, Operator! =, Operator <, operator <=, operator>, operator> =. For operator = and operator! =. If a vector object has the same number of elements and all the elements at the corresponding position are equal, the two vector objects are equal; otherwise, they are not equal. Operator <, operator <=, operator>, and operator> = are compared using dictionary sorting policies. Note: you only need to implement operator = and operator! =. The others can be implemented based on these two implementations. Because Operator! = (LHS, RHS) is! (LHS = RHs), operator <= (LHS, RHS) is! (RHS <LHS), operator> (LHS, RHS) is (RHS <LHS), operator >=( LHS, RHS) is! (LHS, RHS ). In addition to common prefix auto-increment operators, the vector iterator also supports arithmetic operations such as it + N, IT-N, and it2-it1. Note that the return value of it2-it1 is difference_type (signed type ). Note that any operation that changes the container size may invalidate the previous iterator. Application Example # include <iostream> # include <cassert> # include <vector> using namespace STD; int main () {vector <string> V (5, "hello "); vector <string> V2 (v. begin (), V. end (); Assert (V = V2); cout <"> before operation" <Endl; For (vector <string>: const_iterator it = v. begin (); It <v. end (); ++ it) cout <* It <Endl; V. insert (v. begin () + 3, 4, "Hello, world"); cout <"> after Insert" <Endl; For (vector <string>: size_ty PE I = 0; I <v. size (); ++ I) cout <V [I] <Endl; vector <string >:: iterator it = v. erase (v. begin () + 3, V. begin () + 6); Assert (* It = "Hello, world"); cout <"> after erase" <Endl; For (vector <string> :: size_type I = 0; I! = V. size (); ++ I) cout <V [I] <Endl; Assert (v. begin () + v. size () = v. end (); Assert (v. end ()-v. size () = v. begin (); Assert (v. begin ()-v. end () =-vector <string>: difference_type (v. size (); Return 0 ;}Program Note: The above program uses three elements in the loop output container. The traversal mode of each loop is different. In particular, the second loop uses the size () function in condition judgment, instead of retaining the variable before the loop. There are two reasons for doing this: first, if the number of container elements is modified in the loop when the program is modified in the future, this loop can still work well, however, it is incorrect to save the size () function value first. Second, because these small functions (which require only one Return Statement) are basically declared as inline, therefore, you do not need to consider efficiency issues.