C + + linear sequence container <vector> Simple summary
The vector is a variable-length array, which is used without the need to declare the upper bound, and as the element increases, the length of the vector increases automatically; the vector class provides an extra way to add and remove elements, which is more efficient than array operations.
Header files: #include <vector>
Namespaces: Using Namespace Std:vector
constructor function
vector<int>vec_int; Create a vector of a shaping element
vector<string>vec_string; Creates a vector of a string element
vector<mystruct>vec_mystruct; Create an element that is a vector of structures
Basic maintenance Operations 1. Access
Subscript access and at () two ways, the second method is recommended, it will be the boundary check.
Vec_int[1], vec_int.at (1);//access the element with index 1, element 2nd
int size () const; Returns the number of elements in a vector
BOOL empty () const; Determines whether the vector is empty, true is empty, false is not empty
Reference back (); Returns a reference to the last element of the vector, which can be modified
Reference Front (); Returns a reference to the first element of the vector, which can be modified
2. Add
void push_back (const t& Val);; adds an element at the end of the vector.
3. Delete
void Pop_back (); deletes the element at the end of the vector .
void Clear (); Clears all elements in the vector
4. Insert
void Insert (iterator it, int count, t& value); Inserts the value of the specified count number at the position of the iterator, with the count parameter optional, which defaults to 1
5. Traverse
It is also very convenient to use the iterator to traverse it frequently.
Iterator begin (); An iterator that returns the first element of a vector
Iterator End (); returns an iterator to the last element of a vector
Traversal method:
for (vector<int>::iterator vec_it = Vec_int.begin (); Vec_it! = Vec_int.end (); vec_it++) {< < *vec_it << Endl;}
member functions
Assign |
Clears the vector and copies the specified elements to the empty vector. |
At |
Returns a reference to the element at the specified position in the vector. |
Back |
Returns a reference to the last element in the vector. |
Begin |
Returns a random-access iterator to the first element in the vector. |
Capacity |
Returns the number of elements that a vector can contain without allocating more storage. |
Cbegin |
Returns a random-access const iterator that points to the first element in a vector. |
Cend |
Returns a random-access constant iterator that points to the position just above the end of the vector. |
Crbegin |
Returns a const iterator that points to the first element in a reverse vector. |
Crend |
Returns a const iterator that points to the end of the inverse vector. |
Clear |
Clears the elements of the vector. |
Data |
Returns a pointer to the first element in the vector. |
Emplace |
Inserts elements of the ground construct into the vector at the specified position. |
Emplace_back |
Adds an in-place constructed element to the end of the vector. |
Empty |
Tests whether the vector container is empty. |
End |
Returns a random-access iterator that points to the end of the vector. |
Erase |
Removes an element or a series of elements from a vector from a specified location. |
Front |
Returns a reference to the first element in the vector. |
Get_allocator |
Returns the object to the allocator class used by the vector . |
Insert |
Inserts an element or multiple elements into the vector at the specified position. |
Max_size |
Returns the maximum length of a vector. |
Pop_back |
Removes the element at the end of the vector. |
Push_back |
Adds an element at the end of the vector. |
Rbegin |
Returns an iterator that points to the first element in a reverse vector. |
Rend |
Returns an iterator that points to the end of the reverse vector. |
Reserve |
Retains the minimum storage length of the vector object. |
Resize |
Specifies the new size for the vector. |
Shrink_to_fit |
Discard additional capacity. |
Size |
Returns the number of vector elements |
Swap |
Swaps the elements of two vectors. |
C + + linear sequence container <vector> Simple summary