Original address: http://hsw625728.blog.163.com/blog/static/3957072820091116114655254/
A The characteristics of various containers
Vector |
Typical sequence container,C + + standard strict requirements for the implementation of the secondary container memory must be continuous, the only can and standard C-compatible STL container, arbitrary elements read, modify with constant time complexity, insert at the end of the sequence, delete is a constant time complexity, But in the sequence of the head insertion, deletion time complexity is O (n), you can insert new elements anywhere, there is a random access function, insert delete operation needs to be considered. |
Deque |
Sequence container, memory is also contiguous, and vector similar, the difference is that in the sequence of the head insert and delete operations is also a constant time complexity , can be inserted anywhere new elements, have random access function. |
List |
Sequence container, memory is not contiguous, arbitrary element access, modification time complexity is O (n), insert, delete operation is a constant time complexity , you can insert a new element at any location. |
Set |
Associative containers, elements do not allow duplication, data is organized into a red-black tree, looking very fast, time complexity is O (LOGN) |
Multiset |
Associative containers, like set, are not allowed to have duplicate elements, with the time complexity O (LOGN) lookup function. |
Map |
The associative container, which consists of a set of {key, Value }, is organized into a red-black tree by key, finding the time complexity O (LOGN), where the key does not allow repetition. |
Multimap |
As with map, the difference is that the key can be repeated |
Two A classification of containers
Container for contiguous memory: This type of container contains vectors,deque. The feature is that the data is stored on a contiguous block of memory, so when data is inserted and deleted, it is very expensive if it is not at the end of the sequence or at both ends, because of the need to guarantee continuous memory, to make room for new elements, or to populate the space for deleting elements. If you are storing complex structures, it takes a lot of time to copy operations (you can store pointers to complex structures to compensate for this flaw, as discussed in a separate summary).
Node-based containers: Such containers are the remaining list,set,multiset,map, Multimap. The data in this type of container is stored in different memory blocks, which may or may not be contiguous (generally not considered sequential), so that the container modifies the node's pointer when inserting the deleted element, so the consumption is very small.
three. some factors to consider in the use
In the process of use, the issues to be considered are element sequence, standard consistency, iterator capabilities, memory layout and C compatibility, and lookup speed These, considering these issues the container you choose should be very suitable for your current situation.
1. you need to add a lot of new elements :
Vector is the biggest problem when adding elements, because one of his most common memory allocation implementations is to request a new memory space of twice times the current capacity (capacity) , and then copy all the old elements into the new memory. Add a large number of elements when the cost of staggering large. If you have to use vectors for other reasons, and you also need to add a lot of new elements, you can use the member function reserve to allocate memory beforehand, which can reduce the amount of unnecessary consumption.
The list's ability to adapt to this situation is very good, which is the insertion consumption of constant time. Deque said earlier that he was a compromise between the vector and the list, requesting a new memory without enough memory, but not copying the old elements.
2. Search Speed :
This factor depends primarily on the algorithm, and the algorithm ultimately acts on the elements in the container, so the search speed here refers to the best search efficiency the container can achieve.
There are two scenarios for a sequence container, based on whether the elements are sorted,1) for sequenced sequence containers, using binary_search,lower_bound,upper_bound,Equal_ Range can be used to find the log time complexity (O (Logn)), 2) and the unsorted sequence container binary lookup is definitely not, the best time complexity can be achieved is linear (O (n)).
For associative containers, storage is stored as a red-black tree (a more stringent balanced binary tree , which is presented at the end of the document ), and is always able to achieve logarithmic time complexity (O (LOGN)) efficiency because the associative container is ordered according to the key value.
3. whether it is contiguous memory :
There is a clear drawback to the container of contiguous memory, that is, when new elements are inserted or old elements are deleted, in order to make room for new elements or to fill the gap of old elements, the same piece of memory in the other data needs to be shifted overall, the copy cost of this shift is sometimes very large. Vectors in the standard container, deque are contiguous memory, where vectors are fully contiguous memory, and Deque is a compromise implementation of vectors and lists, consisting of multiple memory blocks, The elements in each block are contiguous memory, and the blocks of memory are joined together like linked lists.
Therefore, it is necessary to consider whether there is a need to insert elements at any place in the process of operation, so as to avoid the use of continuous memory vectors,deque
4. Ordering of elements :
The elements in the sequence container are not automatically sorted, what order the programmer inserts in memory, and the associative container is not, and he will sort by an equivalence relationship (equivalence) with his own key value . So the elements in the sequence container are unordered by default, and the elements in the associative container are ordered.
Therefore, when the container is traversing the elements, the sequence of the sequential container output is consistent with the order of insertion, and the associated container is not necessarily. Two examples are given below:
The output results are as follows:
By example, we see that the sequence container vector traversal order is the same as the order of insertion, and the associative container set re-organizes the inserted elements in some order, so when selecting a container, if you care about the insertion order, select the Sequence container.
5. is the memory compatible with C :
The suitable container has only one vector, meaning that if you need to put the data in the container into the array of type C then you do not need to do extra complex operations, if you have vector<int> V, you need to directly use &v[0] You can get a pointer to the first element in V because the memory layout of vector and C arrays is the same, and this requirement is also the standard set by the standard C + + committee. So if the container with such characteristics is only a vector, then the data in the STL container other than the vector should be transformed into a C array form, or the C array is placed in other types of containers, Vector as a bridge, here is an example:
Assume the function void Read (const int* pInt, unsigned int num);
reads num int data starting from the pint pointer position
Std::set<int> MSet;
...//omit operation to mset insert element
Std::vector<int> Mvector (Mset.begin (), Mset.end ());
If (!mvector.empty ())
Read (&mvector[0], mvector.size ());
Four. Advantages and disadvantages of various containers:
The choice of which container to use looks very cumbersome, and if you have an approximate model of each container in your mind, it will be easier to choose.
1. the vector's data model is an array.
Benefits: Memory and C are fully compatible, efficient random access, space saving
Cons: Internal insert Delete elements are expensive, dynamic size check their capacity needs to request a large amount of memory to make a large number of copies.
2. list data structure model is linked list
Pros: Insert delete element in any position constant time complexity, two container fusion is a constant time complexity
Cons: Random access is not supported and more storage space is consumed than vectors
3. The Deque data model is the tradeoff between arrays and linked lists:
Advantages: Efficient random access, internal insertion Delete elements efficient, push pops at both ends
Cons: High memory consumption
4. Map,set,multimap,multiset data structure model is two fork tree (Red black tree )
Pros: Elements are sorted by key value, lookup is logarithmic time complexity, element is checked by key value,map provides subscript access
Five What should I use for a comprehensive look?
First of all, vector,list,deque .
1) If random access is required, use vector
2) If the number of stored elements is known, use a vector
3) You need to randomly insert and delete any position, use list
4) Only need more to insert the deletion element at the end of the container, with deque
5) elements are complex structures with a list, you can also use vectors to store pointers (need extra effort to maintain memory ), to see the requirements
6) If the operation is based on a key value, use the set map
7) If you need a regular search, use the map set
8) The difference between the map set is that the elements in the map are Pair<key, Value>, and the map provides subscript access [], also a trap, this once in the log
Application of Stl-stl containers