Ordered container: vector, deque, list, vectordeque
1. Ordered containers: vector, deque, list
Containers share public interfaces. You only need to learn one type and use another type. Each container provides a set of different time and functions. Generally, you do not need to modify the code, order change type declaration, and each container type replaces another container type, the program performance can be optimized. Container values define a small number of operations. Most additional operations are provided by the algorithm library.
2. To use an ordered container, you must include the header file:
1 #include<vector>2 #include<list>3 #include<deque>
3. All containers are class templates. to define a container, add a pair of angle brackets after the container is specified, and put the element type in the angle brackets:
1 vector<string> vec;2 list<int> lis;3 deque<string>;
All container types define the default constructor to create empty container objects of the specified type. The default constructor does not contain parameters.
Note: When the element type is container type, spaces must be added between angle brackets and element types.
1 vector< vector<int> > vec;
4. Container Initialization
Initialization |
Explanation |
Trial Scope |
C <T> c |
Create an empty container named c |
All containers |
C <T> c (c2) |
Create a copy of container c2 c; c2 and c must have the same container type and store elements of the same type |
All containers |
C <T> c (B, e) |
Create c. Its elements are copies of the elements in the range indicated by iterators B and e. B and e can be other container types with the same element type. |
All containers |
C <T> c (n, t) |
Create n containers whose values are t. t must be T type or can be converted to this type. |
Sequential container |
C <T> c (n) |
Create a container c with n initialization Elements |
Sequential container |
1 vector <int> vec (10, 1); 2 vector <int> vec1 (vec); // OK3 list <int> l (vec); // error, this initialization method requires that the container type and element type are the same 4 list <int> l (vec. begin (), vec. end (); // OK. You can use the iterator to indirectly assign values between different containers with the same element type.
5. Element type constraints
Minimum requirements:
- The element type must support the value assignment operation.
- Element-type objects must be able to be copied
Reference cannot be an element type, because reference does not support assignments of General meanings.
Standard input/output (I/O) cannot be of the element type, because copying or assigning operations are not supported, but other standard library types can
Some container operations have special requirements on the element type. We can define the type of containers, but these operations that have requirements on the element type cannot be used. Assume that class Foo does not have a default constructor, but an int-Type constructor is provided. The following statements are available:
1 vector <Foo> vec; // OK, no need to initialize element 2 vector <Foo> vec (10); // error, create the container vec with 10 initialization elements, but Foo does not have the default constructor 3 vector <Foo> vec (); // OK. Foo provides the constructor of the int type.
Note: When the created container contains element initialization, the element type must provide the corresponding constructor.
6. iterator
Like the container type, all iterators have the same interface. When an iterator supports some operation, other iterators that support this operation will support this operation in the same way.
Common iterator operations:
Operation |
Explanation |
Applicability |
* It |
Returns the reference of the element pointed to by the iterator. |
All containers |
It-> mem |
Dereference it to obtain mem, which is equivalent to (* it). mem. |
All containers |
++ It, it ++ |
It adds 1 to point to the next element of the container |
All containers |
-- It, it -- |
It minus 1, pointing to the previous element of the container |
All containers |
It1 = it2 |
Compare whether two iterators are equal. When the two iterators pointSame containerInSame Element, Or when the next position exceeds the end, the two iterators are equal |
All containers |
It1! = It2 |
>,<, <=,> = |
When an iterator points to an element in the container and points to another iterator, the previous iterator is smaller than the next iterator. |
Vector, deque |
The iterators of the vector and deque containers support arithmetic operations and all relational operations. Because only these two containers support fast and Random Access to elements, therefore, maxcompute can effectively implement arithmetic and relational operations.
Note: The list iterator does not support arithmetic operations and division = ,! = Relational operations other
7. Type alias defined by the container
Size_type |
Unsigned integer |
Iterator |
Container Type iterator |
Const_itreator |
Element read-only iterator |
Reverse_iterator |
Iterator for addressing elements in reverse order |
Constst_reverse_iterator |
Element read-only backward iterator |
Difference_type |
Signed integer that stores the difference between two iterators |
Value_type |
Element type |
Reference |
The left Value Type of the element, which is value_type &'s consent |
Const_reference |
Same as const value_type |
8. Container member functions
Member Functions |
Description |
Return Value |
C. begin () |
Point to the first element of the container |
Iterator |
C. end () |
Point to the next position of the last element of the container |
Iterator |
C. rbegin () |
Point to the last element of the container |
Backward iterator |
C. rend () |
Point to the position before the first element |
Backward iterator |
C. push_back (t) |
Insert element t at the end of the container |
Void |
C. push_front (t) |
Insert element t at the container front end; only applicable List, deque |
Void |
C. insert (p, t) |
Insert a new element whose value is t before the element pointed to by iterator p |
Iterator pointing to new elements |
C. insert (p, n, t) |
Insert n new elements whose values are t before the element pointed to by iterator p |
Void |
C. insert (p, B, e) |
Insert an element in the range marked by iterator B and e before the element pointed to by iterator p. |
Void |
C. size () |
Returns the actual number of elements in the container. |
Number of container elements |
C. max_size () |
Returns the maximum number of elements that container c can accommodate. |
Maximum number of elements |
C. capacity () |
Returns the container size,> = c. size (); obtains the total number of elements that can be stored before the container needs to allocate more storage space. |
|
C. reserve () |
The reserved space of the container is not reallocated until the reserved space is used up. |
|
C. empty () |
Judge whether the container is empty |
Bool Value |
C. resize (n) |
Adjust the container length. When n <c. size (), the redundant elements are deleted. Otherwise, a new element initialized with a value is added. |
Void |
C. resize (n, t) |
Adjust the size of container c. When n> c. size (), the newly added element value is t |
Void |
C. back () |
Returns the reference of the last element. If the container is empty, this operation is not defined. |
Reference of the last element |
C. front () |
Returns the reference of the first element. If the container is empty, this operation is not defined. |
Reference of the first element |
C [n] |
Returns the reference of an element whose subscript is n. It is applicable only to deque and vector. Running Error |
References to elements whose subscript is n |
C. at [n] |
Returns the reference of an element whose subscript is n. It is applicable only to deque and vector. Out-of-bounds, RUN error, and throw exception |
References to elements whose subscript is n |
C. erase (p) |
Delete the element pointed to by iterator p, and return an iterator pointing to the element after the deleted element, such as pointing to the last element, the returned iterator points to the next position beyond the container end. If p points to the next position beyond the end of the container, this operation is not defined. |
Returns an iterator. |
C. erase (B, e) |
Delete all elements in the range marked by iterators B and e, excluding the elements pointed to by e. |
Returns an iterator (the iterator must be the same as above) |
C. clear () |
Delete all elements in c |
Void |
C. pop_back () |
Delete the last element of c. |
Void |
C. pop_front () |
Delete the first element of c, only applicable to list and deque containers |
Void |
Find (B, e, val) |
Find the element val in the range of B and e in the iterator, and return the iterator pointing to the first element found. If not, return the iterator pointing to the next position at the end of the container; use of find must include # Include <algorithm> |
Iterator pointing to the first element |
Count (B, e, val) |
Find the number of val elements in the range of iterator B and e. Use count to include # include <algorithm> |
Number of returned Elements |
C1 = c2 |
Delete all elements in container c1 and copy the elements of c2 to the c1.c1 and c2 types. |
|
C1.swap () |
The elements stored by the Exchange iterator are not invalid. |
Void |
C. assign (B, e) |
Resets the elements of c and copies the elements in the range of iterator B and e to c. B and e cannot be the iterator pointing to elements in c, because the content in c will be deleted before resetting. |
Void |
C. assign (n, t) |
Resets the content of the c container to n t |
Void |
Note: The difference between capacity and reseve is that capacity inserts new elements, even if there is still more space available, but when the reserved space is set with reseve, before the space is not full, capacity will not continue to increase. Generally, reseve and capacity are used at the same time to avoid the consumption of space allocated multiple times during the container process;
9. iterator failure
Insert |
Insert an element in vector and deque, pointing to the iterator behind the newly inserted element is invalid. |
Resize |
If resize compresses the container, the deleted element iterator becomes invalid. |
Value assignment operation c1 = c2 |
All iterators performing the operands are invalid. |
Assign |
All iterators are invalid. |
Note: swap will not be invalid as an iterator
10. Container Selection
Vecetor and deque support random access, but the elements will be moved when the erase and insert Elements
List does not support random access, but elements do not need to be moved when deleting elements.