1. Space Distributor
Std::alloc is used for allocation and deallocation of memory space in containers, and for management of allocated memory. Global functions such as construct (), destroy () are used for the construction and destruction of objects.
2, iterators and trains
Iterators associate a container with an algorithm, behaving like a pointer. Each container implements its own iterator, and most importantly, overloads the operator* and operator->. Each iterator defines a number of types, including the type of the object being referred to. Trains to extract these types.
STL The container can be divided into the following major categories :
One: Sequence container, with vector, List, deque.
two : Associative containers , with set, Multiset, map, Mulmap
Hash_set,hash_map, Hash_multiset, Hash_multimap
A sequence container is a container that can be ordered, and note that it is not already ordered. Vectors, for example, can record the order in which the arrays are entered by push_back.
(1) Vector
Internal data structure: array.
Random access to each element, the time required is constant.
The vector's iterator is invalidated when the memory is reassigned (the element it points to is no longer the same as before or after the operation). When more than capacity ()-size () elements are inserted into the vector, memory is reassigned, all iterators are invalidated, otherwise iterators that point to any element after the current element are invalidated. When an element is deleted, an iterator that points to any element after the deleted element is invalidated.
When adding a new element, if there is enough space left to add, if the remaining space is not enough, you need to open up a new piece of memory (twice times the original memory), then copy the old memory into the new memory, add the elements, and release the memory.
(2) Deque (double-ended queue)
Internal data structure: a segment Array (space). A contiguous space is also allocated to manage these arrays.
Random access to each element, the time required is constant.
Adding any element will invalidate the deque iterator. Deleting an element in the middle of a deque invalidates the iterator. when you delete an element at the head or tail of a deque, only the iterator that points to that element is invalidated.
The default underlying data structure for stack (heap) and queue (queue) is that Deque,stack is only one end in and out; The default is Deque, which can be implemented with list
(3) List
Internal data structure: doubly linked list.
An element cannot be accessed randomly.
Adding any element does not invalidate the iterator. When you delete an element, other iterators are not invalidated except for the iterator that points to the currently deleted element.
Summarize:
1. If you need efficient immediate access, without the efficiency of insert and delete, use vector
2, if you need a large number of insertions and deletions, and do not care about the immediate access, you should use the list
3, if you need to immediately access, and care about the insertion and deletion of data at both ends, you should use Deque
(3) Set map
Based on the red-black Tree (Rb-tree), the time complexity of finding (inserting, deleting) is logarithmic O (logn). Custom key-value types require overloading the < operator because set and map are sorted.
If the element that the iterator points to is deleted, the iterator is invalidated. Any other operation that adds or removes an element does not invalidate the iterator.
(3) Hash_set Hash_map
The bottom layer uses hash_table,vector as an array, and the open-chain method solves the hash conflict. The key value type implements the hash function, and some built-in types have a default hash function in Hash_table, but the custom type does not. So the appropriate array size, the hash function has a great impact on performance.
Compared to set, map:
1. Find (insert, delete) may be faster: in the case of "no collision", in other words, is to have a good enough hash function, it should be able to make the key to the value of the mapping is enough uniform, think it is O (1) level, otherwise, in the worst case, its calculation will degenerate to O (N) level, Become the same as the linked list.
2. Need more memory: through the hash table to speed up the search process, the key to the stored data through the mapping function into an array (usually vector) index, STL is open-chain method to solve, each element of the array to maintain a list, he put the same index value of the data into a list But more memory is needed to store the hash elements, so it can be considered as a space to exchange for time strategies.
STL Container Summary