Summary of C + + STL library and its realization principle __c++

Source: Internet
Author: User

STL containers can be grouped into the following broad categories:
One: Sequence container, vector, list, deque, String.

II: Associated containers, with set, Multiset, map, Mulmap

Hash_set,hash_map, Hash_multiset, Hash_multimap

Third: Other miscellaneous: Stack, queue, Valarray, Bitset

Implementation of various containers of STL:

(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.
You can dynamically increase or decrease elements, and memory management is done automatically, but programmers can use the reserve () member function 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 reassigned and all iterators are invalidated, otherwise the iterator of any element that points to the current element will fail. 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 invalidate the deque iterator. 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 features are similar to the list.

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

(6) Queue
Adapter, which converts any type of sequence container into a queue, typically using 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 a vector as the underlying storage method.
Only the first element can be accessed, and the entire priority_queue cannot be traversed.
The first element is always the one with the highest precedence.

(8) Set
Sort storage by key, values must be comparable and can be understood 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 are the same as set.

(a) Hash_set
Compared to set, the elements inside it are not necessarily sorted, but are assigned according to the hash function used to provide faster search speed (of course, related to the hash function). Hash_set key to hash, and then put the key in the hash value corresponding to the bucket, the principle can be understood in this way, hash_set is key, equal value Hash_map
Other features are the same as set.

(one) Hash_multiset
Keys can be not unique.
Other features are the same as hash_set.

(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 are the same as map.

(HASH_MAP)
Compared to the map, the elements inside it are not necessarily sorted by key values, but are assigned according to the hash function used to provide faster search speed (and, of course, with the hash function).
Other features are the same as map.

(Hash_multimap)
Keys can be not unique.
Other features are the same as hash_map.

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.