The characteristics of sequential containers and associated containers &stl a rough comparison of each container

Source: Internet
Author: User
Tags repetition

Characteristics of sequential containers and associated containers
1. All associative containers are sorted automatically, but map and set do not allow repeating elements, while multimap and multiset allow repeating elements
2. In the associative container, the key and Val of map and Multimap are separate, while the set and Multiset key and Val are the same
3. In addition to the vector and deque iterators are random iterators, the rest of the container iterator is a bidirectional iterator, cannot do the + + operation
4. The three forms of the insert function of the sequential container are:
Iterator insert (iterator position, Value_type &val)
Iterator insert (iterator position, Size_type N, Value_type &val)
Iterator insert (iterator position, iterator first, iterator last)
The three forms of the insert function for the associative container map and set are:
Iterator insert (iterator position, Value_type &val)
Pair<iterator, bool> Insert (Value_type &val)
void Insert (iterator first, iterator last)
The three forms of the insert function of the associative container multimap and multiset are:
Iterator insert (iterator position, Value_type &val)
Iterator Insert (Value_type &val)
void Insert (iterator first, iterator last)
You can see the three forms of the insert function of the associative container, preserving the form of iterator insert (iterator position, Value_type &val) like the sequential container ( The purpose of preserving this form may be to improve insertion efficiency, but since the associative container is automatically sorted, there are no insertion positions specified in the next two forms, i.e. iterator, but in the case of associative containers:
2.1) for map and set, because duplicate elements are not allowed, the second form returns a value of Pair<iterator, Bool&gt, to determine if the element is duplicated
2.2) for Multimap and multiset, because duplicate elements are allowed, the second form returns a value of iterator without judging whether the element repeats
5. The erase function of the sequential container is in the form of:
Iterator Erase (iterator positioin)
Iterator Erase (iterator first, iterator last)
The erase function of the associative container is in the form of:
Iterator Erase (iterator position)
Iterator Erase (iterator first, iterator last)
Iterator Erase (Key_type &val)
6. For inserting and deleting a certain range of elements, the range is [First,last]
7.begin () refers to the first element, and end () refers to the next bit of the last element; Rbegin () refers to the last element, Rend () refers to the first element of the previous
8. For element lookup, the function of the sequential container is the element that returns the specified position, i.e. reference at (size_type N)
For element lookups, the function of the associative container is the iterator that returns the specified key, iterator find (Key_type &val)
Note: The list does not have an at () function because the list cares about inserting and deleting this dynamic operation, and if you need at, you can use vectors, while vectors are concerned with sequential access to their elements or random access
9. The associated container has several associated functions, which are equal_range (), Lower_bound (), Upper_bound (), Key_comp (), Value_comp (), and the reason why these functions are associated is because Equal_ Range () returns two iterators based on Lower_bound () and Upper_bound (), while Lower_bound () and Upper_bound () are based on Key_comp ()
10.list has several interesting functions, which are sort (), remove_if (), unique (). Sort () can be sorted by default (sorted by alphabet, uppercase in lowercase) or by specifying sort functions; unique () can delete the same element or delete the element according to the specified comparison function; remove_if () can delete elements based on the specified function
Note: The rule specified in sort (), remove_if (), unique () can be a function or a class/struct object, and if it is a class/struct object, the class/struct needs to be overloaded operator ()


A rough comparison of the STL containers

Reprint: http://blog.csdn.net/dinghqalex/article/details/44194783
1.vector
Variable length one-dimensional arrays, contiguous memory blocks, memory reserved, memory allocated in the heap;
Support [] operation, high-efficiency random access;
In the last addition of the elements, it is generally not necessary to allocate memory space (because there is reserved memory), fast, in the middle or start the operation of the elements of the memory copy, low efficiency;
Vector is efficient because it configures more memory than it can hold (that is, it has reserved memory), and memory reconfiguration takes a lot of time;
Note: Efficient random access is required instead of inserting and deleting, using vectors
2.list
Doubly linked list, memory space may be discontinuous, no reserved memory, the heap allocates memory;
Random access is not supported, start and end elements are accessed faster, other elements are O (n)
Inserting and deleting elements at any location is faster, and insert and delete operations do not invalidate the individual pointer, reference, iterator of other elements
Note: Large number of insertions and deletions without concern for random access, using list
3.deque
The double-ended queue allocates memory on the heap, a heap holds several elements, and the heap uses pointers to connect;
Supports the [] operation, inserts and deletes elements faster, inserts and deletes in the middle are slower, like a combination of vectors and lists
Note: Care to insert and delete and care about random access, compromise the use of deque
4.map&multimap
Dictionary library, a value mapped to another value, using balanced binary tree storage, the key value is sorted according to the given collation;
The key value in map does not allow repetition, and the key value in Multimap allows repetition;
Find elements faster based on known key values;
5.set&multiset
Ordered set, using balanced binary tree storage, sorting the data in set according to the given collation;
Duplicate elements are not allowed in set, duplicate elements are allowed in multiset;
Find elements faster;

The characteristics of sequential containers and associated containers &stl a rough comparison of each container

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.