Six STL components-containers and six stl components
Containers in STL mainly involve vector, list, And deque, and sequential container adapter types: stack, queue, and priority_queue. Containers in the standard library are divided into sequential containers and associated containers. Elements in the sequential container are stored and accessed by location. As the name suggests, these internal elements are stored sequentially. The order of elements in the ordered container is irrelevant to the element value, it is determined by the order in which elements are added to the container. The elements associated with the container are sorted by keys.
The container class shares some public interfaces. The standard library defines three sequence container types: vector, list, And deque. The differences are only in the way of accessing elements and the cost of adding or deleting element-related operations. Ordered container adapters include stack, queue, and priority_queue. The container only defines a small number of operations, most of which are provided by the algorithm library. If the two containers provide the same operation, their interfaces (function names and number of parameters) should be the same.
Standard containers |
Description |
Ordered container |
Vector |
Quickly insert and delete elements from the backend to directly access any elements |
Deque |
You can quickly insert and delete elements from the front or back to directly access any element. |
List |
Double-stranded tables, fast insertion and deletion from anywhere |
Associated container |
Set |
Quick search, repeated values not allowed |
Multiset |
Quick search, allowing repeated values |
Map |
One-to-multiple ing, fast search based on keywords, repeated values are not allowed |
Multimap |
One-to-multiple ing, fast search based on keywords, allowing repeated values |
Container Type:
Vector |
Container, supporting fast Random Access (continuous storage) |
List |
Linked List, supporting fast insertion/deletion |
Deque |
Dual-end queue, supporting Random Access (continuous storage), fast insertion and deletion at both ends |
Stack |
Stack |
Queue |
Queue |
Priority_queue |
Priority queue |
The following table lists the operations provided by the iterator for all container types:
* Iter |
Returns the reference of the element to which the type iter points. |
Iter-> mem |
Dereference iter and obtain the specified member. |
++ Iter |
Add 1 to iter to point to the next element in the container |
Iter ++ |
|
-- Iter |
Subtract 1 from iter to point to the previous element in the container. |
Iter -- |
|
Iter1 = iter2 |
When two iterators direct to the same element in the same container or |
Iter1! = Iter2 |
When the next position of a container exceeds the end, the two iterators are equal. |
The iterator of the vector and deque containers provides additional operations: the arithmetic operations of the iterator and other relational operations, as shown in the following table:
Iter + n |
Adding (subtract) an integer to the iterator generates an iterator pointing to the nth element in the container; |
Iter-n |
The new iterator must point to the elements in the container or the next position beyond the container end. |
Iter1 + = iter2 |
Compound operation: First add (subtract) and then assign a value |
Iter1-= iter2 |
|
Iter1-iter2 |
Applicable only to vector and deque |
>,>=, <,<= |
Compare the positional relationship of the iterator; applicable only to vector and deque |
Relational operators are only applicable to vector and deque containers, because only these two containers provide fast and random access to their elements. They ensure that the specified container elements can be accessed directly and effectively based on the element location. Both containers support random access through element positions, so their iterators can effectively implement arithmetic and relational operations.
Iterator range: [first, last) is a left closed range, indicating that the range starts from first to last, but does not include last. NOTE: If first is not equal to last, the auto-increment operation on first must be able to reach last; otherwise, that is, the last is before first, and undefined behavior will occur.
The range of the iterator uses left closed: Because this can uniformly represent empty sets, no special processing is required.
In addition, when using the iterator, pay special attention to the possible failure problems of the iterator.
Access element:
Back () |
Returns the reference of the last element of the container. If the container is empty, this operation is not defined |
Front () |
Returns the reference of the first element of the container. If the container is empty, this operation is not defined |
C [n] |
Returns the reference of an element whose subscript is n. If n <0 or n> = size (), this operation is not defined. (Note: Only applicable to vector and deque containers) |
At [n] |
Returns the reference of an element whose subscript is n. If the subscript is invalid, an exception out_of_range is thrown. (Note: Only applicable to vector and deque containers) |
Delete element:
Erase (p) |
Delete the element pointed to by iterator p. Returns an iterator pointing to the elements behind the deleted element. If p points to the last element in the container, the returned iterator points to the next position of the container beyond the end. If p itself is the iterator pointing to the next position beyond the end, the function is not defined. |
Erase (B, e) |
Delete all elements in [B, e. Returns an iterator pointing to the elements behind the deleted element segment. If e itself is an iterator pointing to the next position beyond the end, the returned iterator also points to the next position beyond the end. |
Clear () |
Delete all elements in the container and return void |
Pop_back () |
Delete the last element in the container and return void. If the container is empty, this operation is not defined. |
Pop_front () |
Delete the first element in the container and return void. If c is an empty container, this operation is not defined. (Note: Only applicable to list and deque containers) |
Assignment and swap:
C1 = c2 |
Delete all elements of container c1 and copy the elements of c2 to container c1. C1 and c2 must be of the same type. |
C1.swap (c2) |
Exchange Content: after this function is called, c1 stores the original c2 elements, and c2 stores the original c1 elements. C1 and c2 must be of the same type. The execution speed of this function is usually faster than copying c2 elements to c1. |
C. assign (B, e) |
Re-set the elements of c: Copy all the elements in the range marked by iterator B and e to c. B and e must not be the iterator pointing to elements in c. |
C. assign (n, t) |
Reset container c to store n elements whose values are t. |
Note: The assign operation first deletes all elements in the container, and then inserts the new elements specified by the parameter into the container.
The swap operation does not delete or insert any elements, and ensures exchange within the constant time. The iterator does not expire because no elements are moved in the container. Note that these iterators point to elements in another container.
Container selection:
The vector and deque containers provide quick access to elements, but the cost is that inserting or deleting elements anywhere in the container is more costly than inserting and deleting elements at the end of the container, to ensure its continuous storage, you need to move elements. The list type can be quickly inserted and deleted at any location, because continuous Storage is not required, however, the cost is that the random access overhead of elements is large. The features are as follows:
1) Like a vector container, insert or erase elements in the deque container are less efficient;
2) Unlike the vector container, the deque container provides efficient insert and erase operations in its header, just like at the end;
3) Unlike the vector container, the deque container supports random access to all elements.
4) Deleting an element at the beginning or end of the deque container will only invalidate the iterator pointing to the deleted element. Insert and delete operations at any other location of the deque container will invalidate all iterators pointing to the container element.
Container comparison:
Vector (continuous space storage, you can use the [] Operator) to quickly access random elements, quickly Insert elements at the end, but insert Between the ages of the sequence, it takes a long time to delete an element, and if the space allocated at the beginning is not enough, there will be a performance overhead for re-allocating a larger space and then copying it.
Deque) quick access to random elements, fast insertion of elements at the beginning and end, random insertion, slow deletion of elements, and faster Space reallocation than vector, the original elements do not need to be copied. To sort a deque, copy the deque to the vector first, and then copy it back to deque.
List (each element is connected with a linked list) does not access a random element as fast as a vector, and random inserted elements are faster than a vector. Therefore, space is allocated to each element. Therefore, there is no sufficient space and the allocation is re-allocated.
Set: the internal elements are unique and stored in a balance tree structure. Therefore, the elements are sorted during traversal and the search speed is relatively high.
Map: the combination of one-to-one mappings, and keys cannot be repeated.
Stack: the adapter must be used with other containers. The default internal container in stl is deque. First, only one exit is available, and traversal is not allowed.
Queue: it is a restricted deque. It is easier to use a list for internal containers. First-in-first-out, traversal not allowed.
Vector <bool> and bitset <>. The length can be changed dynamically.
Priority_queue: The inserted element has a priority order, and the top element has the highest priority.
Valarray is specialized in numerical calculation and adds special mathematical functions.
Some container selection rules:
1)If the program requires Random Access to elements, useVectorOrDequeContainer;
2)If the program must insert or delete elements in the middle of the container, useListContainer;
3) if the program is not in the middle of the container, but inserts or deletes elements in the header or tail of the container, deque container should be used;
4) if you only need to insert an element in the middle of the container when reading the input, and then randomly access the element, you can read the element into a list container during the input, sort the containers and then copy the sorted list containers to the vector containers.
5) if the program requires both random access and insertion or deletion of elements in the middle of the container, the impact of which operation should be weighed, to decide whether to select the list container or the vector or deque container. Note: If you choose to use a vector or deque container, you can consider using only the operations they share with the list container, such as using an iterator instead of a subscript to avoid random element access, in this way, you can easily rewrite the program to use the list container when necessary.