Valid STL class Note 1-how to select a proper container

Source: Internet
Author: User

First, container classification:

 

(1) Standard STL sequence containers: vector, String, deque, and list.
(2) Standard STL associated containers: Set, Multiset, map, and multimap.
(3) Non-standard sequence containers slist and rope. Slist is a one-way linked list, and rope is essentially a heavy string.

(4) non-standard associated containers hash_set, hash_multiset, hash_map, and hash_multimap.

 

Next, how to choose a more appropriate container:

 

(1) If you want to insert a new element anywhere in the container, it is more appropriate to select a sequence container.

(2) If we care about the order of elements in the container, avoid using a hashed container.

(3) If you want to randomly access the iterator, it is technically limited to vector, deque, and string.

(4) If we care about the search speed, we should first consider using the hash container, then the sorted vector and the standard associated container.

(5) If we care that the bottom layer of the container uses reference count, we should avoid using it as much as possible. Its underlying implementation is implemented by reference count.

We can consider replacing string with vector <char>.

(6) If you need to support multi-element insertion, you should select list, because list is the only standard container that provides multi-element insertion of transactional semantics.

(7) If you need a sequence container with the following features: 1) You can use a random access iterator; 2) as long as it is not deleted and insertion only occurs at the end of the container, the pointer and referenced data will not expire, so deque is the ideal choice at this time.

Compared with vector, deque also supports inserting data from the start end: push_front ().

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.