An explanation of the use time of STL containers

Source: Internet
Author: User

The C + + standard library provides a variety of different containers with unique features. Now the question is: How do you choose the best container category? The following table gives an overview.

But some of these descriptions may not be practical. For example, if you need to deal with a small number of elements, can be the tiger complexity, because the linear algorithm is usually the process of the element itself is relatively fast, in this case, "explicit complexity with fast element processing" than "logarithmic complexity with slow element processing" is a good deal.

As a supplement to the above table, when used:

1. Vectors should be used by default. Vector's internal structure is the simplest, and allows random access, so the data access is very convenient and flexible, data processing is fast enough.

2. If you often want to insert and remove elements from the head and tail of the sequence, you should use Deque. If you want the element to be removed, the container can automatically reduce the memory, then you should also use Deque. In addition, since vectors usually have a chunk of memory to hold elements, and deque uses multiple chunks, the latter can contain more elements.

3. Consider using list if you need to frequently perform element placement, removal, and movement in the middle of a container. The list provides special member functions that can transfer elements from the A container to the B container within a constant time. However, because the list does not support random access, the performance will be compromised if you only know the element in the list by the head of the list.

Similar to all "node-based" containers, the list does not invalidate iterators that point to those elements as long as the elements or containers are gone. Vector does not, once it exceeds its capacity, all its iterators,pointers.references will be invalidated, when the placement or removal operation, will also make a part of iterators, pointers, references failure. As for Deque, when its size changes, all iterators,pointers,references will fail.

4. If the container you want is of this nature: if the operation is unsuccessful and ineffective, then you should use list or associative container.

5. If you often need to search for elements based on a guideline, you should use the set or multiset to sort the elements with that sort criteria. Remember, in theory, the logarithm complexity is 10 times times better than the linear complexity in the order of 1000 elements. In terms of search speed, hash table is usually 5-10 times faster than a binary tree. But the elements of the hash table are not sorted, so if the element must be sorted, it will not be used.

6. If you want to handle key/value pair, please use map or Multimap.

7. If an associative array is required, map should be used.

8. If a dictionary structure is required, multimap should be used.

An explanation of the use time of STL containers

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.