Stl-stl Container Introduction

Source: Internet
Author: User

Original address: http://www.cnblogs.com/duzouzhe/archive/2010/01/12/1645191.html

The STL container can be divided into the following major categories:
One: Sequence container: Vector, List, deque, String.

II: Associative containers: Set, Multiset, map, Mulmap, Hash_set, Hash_map, Hash_multiset, Hash_multimap

III: Miscellaneous: Stack, queue, Valarray, Bitset

Implementation of various STL containers:

(1) Vector
Internal data structure: array.
Random access to each element, the time required is constant.
The time required to add or delete elements at the end is independent of the number of elements, and the time required to add or delete elements in the middle or at the beginning varies linearly with the number of elements.
Elements can be dynamically added or reduced, memory management is done automatically, but programmers can use the reserve () member function to manage memory.
The vector's iterator is invalidated when the memory is reassigned (the element it points to is no longer the same as before or after the operation). When more than capacity ()-size () elements are inserted into the vector, memory is reassigned, all iterators are invalidated, otherwise 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 add elements 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 decreased, memory management is done 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 a deque invalidates the iterator. When you delete an element at the head or tail of a deque, only the iterator that points to that element is invalidated.

(3) List
Internal data structure: Two-way loop linked list.
An element cannot be accessed randomly.
Bidirectional traversal is possible.
The time required to add or remove elements at the beginning, end, and anywhere in the middle is constant.
Elements can be dynamically added or reduced, and memory management is done automatically.
Adding any element does 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.
Cannot traverse in two directions, only from front to back.
Other features are similar to list.

(5) Stack
Adapter, which converts any type of sequence container to a stack, typically using deque as a supported sequence container.
The element can only last in, first out (LIFO).
You 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.
The element can only be first in and out (FIFO).
You cannot traverse the entire queue.

(7) Priority_queue
Adapter, which converts any type of sequence container into a priority queue, typically using vectors 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 highest-priority element.

(8) Set
Keys and values are equal.
Key is unique.
The elements are sorted in ascending order by default.
If the element that the iterator points to is deleted, the iterator is invalidated. Any other operation that adds or removes an element does not invalidate the iterator.

(9) Multiset
Keys can be not unique.
Other features are the same as set.

(Ten) Hash_set
In comparison with set, the elements in it are not necessarily ordered, but are assigned according to the hash function used, which provides a faster search speed (of course, it is related to the hash function).
Other features are the same as set.

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

(map)
Key is unique.
The ascending order of the element default keys.
If the element that the iterator points to is deleted, the iterator is invalidated. Any other operation that adds or removes an element does not invalidate the iterator.

(Multimap)
Keys can be not unique.
Other features are the same as map.

(HASH_MAP)
In contrast to map, the elements in it are not necessarily sorted by key values, but are dispatched according to the hash function used, which provides a faster search speed (as well as 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.

Stl-stl Container Introduction

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.