Vector C ++, Vector
Vector container type
A 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 Construction
Function 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 with n elements
Vector (const vector & x );
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; // create an empty container whose object type is string
Vector <string> v2 (10); // create a container with 10 string class objects with initial values (that is, empty strings)
Vector <string> v3 (5, "hello"); // create a container of five string class objects with the value "hello"
Vector <string> v4 (v3.begin (), v3.end (); // v4 is the same container as v3 (completely copied)
Vector operations (the following functions are all member functions)
Bool empty () const; // if the container is empty, true is returned; otherwise, false is returned.
Size_type max_size () const; // returns the maximum number of elements that the container can accommodate.
Size_type size () const; // return 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 ()> = n
Void resize (size_type n, T x = T (); // make sure that: size () = n is returned; 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 non-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); // returns a reference to an element whose subscript is pos (subscript starts from 0. If the subscript is incorrect, it is undefined.
Const_reference operator [] (size_type pos) const;
Reference at (size_type pos); // returns the reference of an element whose subscript is pos. If the subscript is incorrect, an exception out_of_range is thrown.
Const_reference at (size_type pos) const;
Void push_back (const T & x); // Add an element to the end of the container
Void pop_back (); // The last element in the pop-up container (the container must be not empty)
// Note: The following insert and delete operations will move elements (to maintain continuous storage), so the previous iterator may become invalid.
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_type n, const T & x); // note that the iterator may no longer be valid (space may be reassigned)
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 vertex is deleted 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 (); // value assignment, which replaces all elements in the container with the specified element sequence
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 ();
Const_reverse_iterator rend () const;
Reverse_iterator rend ();
Comparison of vector objects (non-member functions)
There are six comparison operators for comparing vector objects: operator = and 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 ).
Vector iterator
In addition to common prefix auto-increment operators, the vector iterator also supports arithmetic operations: 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_type 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 Description: 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.