I. Category:
- Standard STL sequence container: Vector, string, deque, and list.
- Standard STL associated container: Set, multiset, map, and multimap.
- Non-standard sequence containerSlist and rope. Slist is a one-way linked list, and rope is essentially a heavy string
- Non-standard associated containerHash_set, hash_multiset, hash_map, and hash_multimap.
- Several TypesStandard non-STL containerIncluding array, bitset, valarray, stack, queue, and priority_queue
It is worth noting that arrays can work with STL algorithms because pointers can be used as arrays as iterators.
Ii. Delete Elements
If you want to delete something, remember to add the so-called erase deletion algorithm after removing the algorithm. In the end, you still need to call the member function to delete an element, but because remove does not know which container it acts on, therefore, the remove algorithm cannot really delete an element. vector vector <int> v;
V. reserve (10 );
For (int I = 1; I <= 10; ++ I ){
V. push_back (I );
}
Cout <v. size (); // 10
V [3] = v [5] = v [9] = 99;
V. erase (remove (v. begin (), v. end (), 99), v. end ());
Cout <v. size (); // 7
2. list
List <int> listTest; listTest. remove (99); // This member function will actually Delete elements, which is much more efficient than erase + remove and remove_if. However, unique behavior is also like remove. It is used to delete items (adjacent duplicate values) from a range without accessing the container holding the range element. If you really want to delete elements from the container, you must call unique and erase in pairs. unique is similar to remove in the list. Just like list: remove actually deletes (and is much more efficient than erase-remove ). List: unique also deletes adjacent duplicate values (also more efficient than erase-unique ).
Three iterators failed:Vector:
1. When an element is inserted (push_back), The iterator returned by the end operation is definitely invalid.
2. When an element is inserted (push_back), the return value of capacity is different from that before the element is inserted. Then, the entire container needs to be reloaded, And the iterator returned by the first and end operations will become invalid.
3. After the delete operation (erase, pop_back) is performed, all the iterators pointing to the delete vertex are invalid. All the iterators pointing to the elements behind the delete vertex are also invalid.
Deque iterator failure:
1. inserting elements at the front or end of the deque container does not invalidate any iterator.
2. Deleting an element at its header or tail will only invalidate the iterator pointing to the deleted element.
3. The insert and delete operations at any other location of the deque container will invalidate all iterators pointing to the container element.
List/set/map1. When deleting a node, the list pointing to the node to which the iterator fails <int> intList;
List <int>: iterator it = intList. begin ();
While (it! = IntList. end ())
{
It = intList. erase (it );
......
}
4. Select the time <> -- summarize the characteristics of various containers(1) vector
Internal data structure: array.
Each element is randomly accessed. The time required is a constant.
The time required to add or delete an element at the end is irrelevant to the number of elements. The time required to add or delete an element at the beginning or in the middle changes linearly with the number of elements.
You can dynamically add or remove elements and manage the memory automatically. However, you can use the reserve () member function to manage the memory.
The iterator of the vector will become invalid when the memory is re-allocated (the elements it points to are no longer the same before and after the operation ). When more than capacity ()-size () elements are inserted into the vector, the memory will be re-allocated and all iterators will become invalid; otherwise, the iterator pointing to any element after the current element fails. When an element is deleted, the iterator pointing to any element after the element is deleted becomes invalid.
(2) deque
Internal data structure: array.
Each element is randomly accessed. The time required is a constant.
The time required to add an element at the beginning and end is irrelevant to the number of elements. The time required to add or delete an element in the middle changes linearly with the number of elements.
Elements can be dynamically added or removed, and memory management is completed automatically. member functions used for memory management are not provided.
Adding any element will invalidate the deque iterator. Deleting an element in the middle of deque will invalidate the iterator. When a deque header or tail deletes an element, only the iterator pointing to the element fails.
(3) list
Internal data structure: Bidirectional Ring linked list.
You cannot randomly access an element.
Bidirectional traversal is supported.
The time required to add or delete an element at the beginning, end, or in the middle is constant.
You can dynamically add or remove elements and manage the memory automatically.
Adding any element will not invalidate the iterator. When an element is deleted, other iterators will not expire except the iterator pointing to the currently deleted element.
(4) slist
Internal data structure: one-way linked list.
It cannot be traversed in two directions. It can only be traversed from front to back.
Other features are similar to list.
(5) stack
Adapter, which can convert any type of sequence container into a stack. Generally, deque is used as the supported sequence container.
The element can only be post-in, first-out (LIFO ).
The entire stack cannot be traversed.
(6) queue
It can convert any type of sequence container into a queue. Generally, deque is used as the supported sequence container.
The element can only be FIFO ).
The entire queue cannot be traversed.
(7) priority_queue
It can convert any type of sequence container into a priority queue. Generally, vector is used as the underlying storage mode.
Only the first element can be accessed, and the whole priority_queue cannot be traversed.
The first element is always the element with the highest priority.
(8) set
The keys and values are equal.
The key is unique.
Elements are arranged in ascending order by default.
If the element to which the iterator points is deleted, the iterator becomes invalid. Any other operations to add or delete elements will not invalidate the iterator.
(9) multiset
The key may not be unique.
Other features are the same as those of set.
(10) hash_set
Compared with set, the elements in it are not necessarily sorted, but allocated by the hash function used. It can provide faster search speed (of course, related to the hash function ).
Other features are the same as those of set.
(11) hash_multiset
The key may not be unique.
Other features are the same as hash_set.
(12) map
The key is unique.
The elements are sorted in ascending order by default.
If the element to which the iterator points is deleted, the iterator becomes invalid. Any other operations to add or delete elements will not invalidate the iterator.
(13) multimap
The key may not be unique.
Other features are the same as those of map.
(14) hash_map
Compared with map, the elements in it are not necessarily sorted by key values, but are allocated according to the hash function used, it provides a faster search speed (of course, also related to the hash function ).
Other features are the same as those of map.
(15) hash_multimap
The key may not be unique.
Other features are the same as hash_map.