Comparison of the C ++ STL container

Container Features:

vector: Typical sequence containers, C ++ standard stringent requirements to implement memory sub-containers must be continuous, reads the only standard C-compatible stl container of any element, modified with constant time complexity, be inserted in the end of the sequence, remove the time complexity is constant, but the sequence is inserted in the head, remove the time complexity is O (n), can be inserted at any position of the new element, random access, insert delete operation to consider.

the deque (deque): SEQ container is also continuous memory, and vector similar, except that the head of the sequence of insertion and deletion operation is also constant time complexity, the new element may be inserted at any position, there is a random access function.

list: sequence containers, the memory is not continuous, access any element, modification time complexity is O (n), insert, delete operations are complicated degree constant time, the new element may be inserted in any position.

set: correlation container element does not allow duplication, the data is organized into a red-black tree, looking very fast speed, the time complexity is O (logN)

multiset: associative containers, and set the same, but do not allow duplicate elements, comprising the time complexity of O (logN) search function

map: association container according {key, value} manner to form a set, by key organized into a red-black tree, to find the time complexity of O (logN), where the key must be unique.

multimap: and map the same, the difference is the key to repeat

classification:

Continuous memory containers: vector, deque

Therefore, data insertion and deletion of time, or if not at the expense of both ends of the sequence so it takes a very large, contiguous memory because of the need to ensure that, at the same time to make room for new elements or remove elements filling the space, if storage is complex structure will spend a lot of time, then copying operation (complex structure may store a pointer to fill this gap, the other one in discussion summary)

Node-based containers: list, set, multiset, map, multimap

Such containers in the insertion and deletion of elements modified only when a node pointer, so consumption is very small.

Considerations:
(1) requires a lot of added elements: vector when a large amount of elements of the biggest problems, list the ability to adapt to this situation is very good, deque (composed of a plurality of memory blocks) As I mentioned before, he is vector and compromise in the form of list, the memory is not enough to apply for a new memory, but does not copy the old elements.

(2) search speed: sequence container vessel has been distinguished, the sort that is good logn, not the best is n, then the associated container, when stored in the storage is a red-black tree (a more stringent balanced binary tree, the document finally have introduced), always achieve the efficiency of the complexity of the number of time (O (logN)), because associative containers are sorted according to key values.

(3) contiguous memory: If you want to insert elements anywhere, then it does not consider the vector, deque

Sorting (4) elements: association readily be sorted according to a certain equivalence relationship

(5) whether and C compatible memory: vector compatible

So the advantages and disadvantages are:

1. Vector data model is an array.

Pros: Memory and C is fully compatible with efficient random access, space-saving

Disadvantages: 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.

2. List of model data structure is a linked list

Advantages: anywhere complexity of insertion and deletion time constant element, two containers fusion is constant time

Disadvantages: does not support random access, take up more memory than vector space

3. Deque data model is an array and a linked list of compromise:

Advantages: efficient random access is easy to delete the internal insert element efficiency, both ends of the push pop

Disadvantages: relatively high memory usage

4. Map, set, multimap, multiset model data structure is a binary tree (red-black tree)

Advantages: key elements will be in accordance sort, search logarithmic time complexity, through the key elements of search, map index provides access

Guess you like

Origin www.linuxidc.com/Linux/2019-06/158955.htm