Basic characteristics and principle of c++stl container __c++

Source: Internet
Author: User

STL the container can be divided into the following major categories :
One: Sequence container, with vector, list, deque, string.

two : Associative Container , There are set, Multiset, map, Mulmap, hash_set, Hash_map, Hash_multiset, Hash_multimap

three : Other Miscellaneous: stack, queue, Valarray, Bitset

STL implementation of each container :

(1) Vector
Internal data structure: array.
Random access to each element, the time required is constant.
The time required to add or remove elements at the end is independent of the number of elements, and the time required to add or remove elements in the middle or the beginning varies linearly with the number of elements.
can dynamically increase or decrease elements, memory management is completed automatically, but programmers can use Reserve () member functions to manage memory.
The vector
's iterator will fail when the memory is reassigned (the element it points to is no longer the same before or after the operation). When the capacity ()-size ( ) element is inserted into the vector , the memory is redistributed and all iterators are invalidated; 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.

(2) Deque
Internal data structure: array.
Random access to each element, the time required is constant.
The time required to increase an element at the beginning and end is independent of the number of elements, and the time required to add or delete elements in the middle varies linearly with the number of elements.
Elements can be dynamically increased or reduced, memory management is completed automatically, and no member functions are provided for memory management.
adding any element will make deque iterator is invalidated. Deleting an element in the middle of the deque will invalidate the iterator. when an element is deleted at the head or end of a deque , only the iterator that points to the element is invalidated.

(3) List
Internal data structure: bidirectional ring linked list.
An element cannot be accessed randomly.
Can be traversed in two directions.
The time required to add or remove elements at the beginning, end, and center is constant.
Can dynamically increase or reduce elements, memory management automatically completed.
adding any element will 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.

(4) Slist
Internal data structure: one-way linked list.
Not bidirectional traversal, can only be traversed from the front to the back.
other characteristics of the same List similar.

(5) Stack
adapter, which converts a sequence container of any type into a stack, typically using the deque as a supported sequence container.
element can only be LIFO first ( LIFO ).
cannot traverse the entire Stack .

(6) Queue
adapter, which converts a sequence container of any type into a queue, typically using the deque as a supported sequence container.
elements can only be advanced first out ( FIFO ).
cannot traverse the entire Queue .

(7) Priority_queue
adapter, which converts a sequence container of any type into a priority queue, typically using the Vector as the underlying storage method.
You can access only the first element, and you cannot traverse the entire priority_queue .
The first element is always the one with the highest precedence.

(8) Set
sort storage by key, values must be comparable, as set is a map with keys and values equal
Keys unique.
The elements are sorted by default in ascending order.
if an iterator points to an element that is deleted, the iterator fails. Any other action that adds or deletes elements does not invalidate the iterator.

(9) Multiset
Keys can be not unique.
Other features and Set The same.

(a) Hash_set
with Set In contrast, the elements inside it are not necessarily sorted, but are based on the Hash function, it can provide faster search speed (of course, with Hash function). hash_set the key to hash and then The key is placed in the bucket corresponding to the hash value, and the principle can be understood in this way, hash_set is the key, the equal value of hash_map
Other features and Set The same.

(one) Hash_multiset
Keys can be not unique.
Other features and Hash_set The same.

(a) Map
Keys unique.
The ascending arrangement of the element default keys.
if an iterator points to an element that is deleted, the iterator fails. Any other action that adds or deletes elements does not invalidate the iterator.

(Multimap)
Keys can be not unique.
Other features and Map The same.

(HASH_MAP)
with Map In contrast, the elements inside it are not necessarily sorted by key values, but by the Hash function, which provides faster search speed (and, of course , Hash function).
Other features and Map The same.

(Hash_multimap)
Keys can be not unique.
Other features and Hash_map The same.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.