Vector1. Internal data structure: continuous storage, such as arrays.
2. Random Access to each element. The time required is a constant.
3. 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.
4. You can dynamically add or remove elements and manage the memory automatically. However, programmers can use the reserve () member function to manage the memory.
5. The iterator is invalid.
Insert: The iterator of the vector will expire when the memory is re-allocated (the elements it points to are no longer the same before and after this operation ). When more than capacity ()-size () elements are inserted into the vector, the memory will be re-allocated and all iterators will become invalid;
Delete: After the delete operation (erase, pop_back) is performed, all iterator pointing to the delete vertex and its elements after the deletion are invalid.
Suggestion: when using vector, use the reserve () member function to pre-allocate the required memory space, which can not only protect the iterator from invalidation, but also improve the running efficiency.
Deque1. Internal data structure: continuous storage or segmented continuous storage, depending on implementation, such as arrays.
2. Random Access to each element. The time required is a constant.
3. 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.
4. elements can be dynamically added or removed, and memory management is automatically completed. member functions used for memory management are not provided.
5. The iterator is invalid.
Insert: adding any element will invalidate the deque iterator.
Delete: 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.
List1. Internal data structure: Bidirectional Ring linked list.
2. One element cannot be randomly accessed.
3. Bidirectional traversal.
4. The time required to add or delete an element at the beginning, end, or in the middle is constant.
5. elements can be dynamically added or removed, and memory management is automatically completed.
6. iterator failure
Insert: adding any element will not invalidate the iterator.
Delete: When an element is deleted, other iterators will not expire except the iterator pointing to the currently deleted element.
Slist1. Internal data structure: one-way linked list.
2. Bidirectional traversal is not allowed. It can only be traversed from front to back.
3. Other features are similar to list.
Suggestion: Do not use insert, erase, and previous operations of slist. Because these operations need to traverse forward, but slist cannot traverse forward directly, it will search backward from the beginning, the time required is proportional to the number of elements located before the current element. Slist provides functions such as insert_after and erase_after for optimization. However, if you need to traverse forward, we recommend that you use list.
Stack1. adapter, which can convert any type of sequence container into a stack. Generally, deque is used as the supported sequence container.
2. The element can only be post-in-first-out (LIFO ).
3. The entire stack cannot be traversed.
4. adapter, which can convert any type of sequence container into a queue. deque is generally used as the supported sequence container.
5. elements can only be FIFO ).
6. The entire queue cannot be traversed.
7. adapter, which can convert any type of sequence container into a priority queue. Generally, vector is used as the underlying storage mode.
8. You can only access the first element and cannot traverse the entire priority_queue.
9. The first element is always the element with the highest priority.
Queue
Priority_queueSuggestion: When you need a data structure such as stack, queue, or priority_queue, directly use the corresponding container class and do not use deque for similar work.
Set1. The keys and values are equal.
2. The key is unique.
3. elements are arranged in ascending order by default.
4. The iterator is invalid.
Delete: If the element to which the iterator points is deleted, the iterator becomes invalid.
Map1. The key is unique.
2. elements are sorted in ascending order by default.
3. iterator failure
Delete: If the element to which the iterator points is deleted, the iterator becomes invalid.
Combination of hash and set, Multiset, map, and multimapThe elements in it are not necessarily sorted by key values, but assigned by the hash function used. It provides faster search speed (currently related to the hash function ).
Suggestion: Set, Multiset, map or multimap should be selected when the element order is more important than search. Otherwise, select hash_set, hash_multiset, hash_map, or hash_multimap.
Suggestion: when inserting elements into the container, if the order of the elements in the container does not matter, add them to the end as much as possible. If you need to add or delete elements at the beginning or in the middle of the sequence container, select list.
Suggestion: Do not use a C-style string (character pointer char *) as the key value for the associated container. If not available, the string comparison operator, namely operator <, operator =, operator <=, and so on, should be explicitly defined.
Suggestion: Use the reference transmission mode when the container is passed as a parameter. Otherwise, the overhead of the copy constructor of the container will be called.
STL creation is independent of the platform, application, and data type.
The Code is as follows:
[CPP]View plaincopy
- Vector <int> VEC;
- Vector <int>: iterator iter = Vec. Begin ();
- Int main (void)
- {
- While (ITER! = Vec. End ())
- {
- Iter = Vec. Erase (ITER); // when the vector is deleted, the iterator becomes invalid. You need to obtain the iterator again.
- }
- Return 0;
- }
STL source code analysis-iterator summary and iterator failure Summary