C ++ STL containers, and select the appropriate operation of the time complexity of the container

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Nash_Cyk/article/details/79102754

C ++ STL plays for each developer crucial role in each container a good package corresponding data structure, so as to ensure that we in the use of safe, stable and efficient vessel, but how to choose the right container in a particular scene, we still need a lot of attention to detail, more effective stl STL the book describes in detail the considerations, to sum up the following:
a large number of new elements are added when not to use vector because the internal implementation is the use of vecter new2 times the original memory then copy space, affecting the efficiency, this time you can select list.
For example, when the container associated lookup map logn time complexity because of its internal implementation uses red-black tree.
Binary search time complexity of non-ordered arrays logn time complexity compared to using n traversal.
If you try to avoid frequent use of vector and deque because of its internal memory is continuous, insert, or delete when you insert the same memory deletes all other data needs to be a whole shift.
Traversal order and sequence when the container is inserted is the same, and associated content might not necessarily, associated with the vessel interior may be ordered automatically processed into the form.
Vector data model is an array, this is fully compatible with C, efficient random access, saving space. But the drawback is the cost of removing elements inserted inside a huge, dynamic size checked their capacity need to apply a lot of memory to do a lot of copies.
List of model data structure is the list, insert anywhere in constant time complexity delete elements, integration of the two containers is constant time complexity On, but does not support random access, take up more memory than vector space.
Deque data model and the list is an array of compromise, efficient random access is easy to delete the internal insert element efficiency, both ends of the push pop, the same memory usage is relatively high.
Map, set, multimap, multiset model data structure is a binary tree (red-black tree), will follow the key elements of sort, search is logarithmic time complexity, through the key elements of search, map index provides access logn.
If the random access required by Vector, if the number of the storage elements is known, with Vector, requires anywhere random insertion and deletion, with the List, only the header portion need more container is inserted into the tail removing elements, with the deque, the elements are complex structures list, you can also use vector memory pointer (requires additional energy to maintain memory), look needs. , If the operation is based on a key, a set map, if required frequent searches, with the map set, if we have the same minimum efficiency rate, hash_map may be utilized, with respect to the space for time, time complexity is O1.

Guess you like

Origin blog.csdn.net/Nash_Cyk/article/details/79102754