STL 7: detailed explanation of the Use Time of STL containers

Source: Internet
Author: User

C ++ StandardProgramThe Library provides different containers with their own expertise. Now the question is: how do I select the best container category? The following table provides an overview.

However, some of the descriptions may not be practical. For example, if you need to handle a small number of elements, the complexity can be reduced because of the linearAlgorithmGenerally, the processing of elements is faster. In this case, "Explicit complexity and quick Element Processing" is more cost-effective than "processing of elements with slow logarithm complexity.

Used as a supplement to the table above:

1. Vector should be used by default. Vector has the simplest internal structure and allows random access. Therefore, data access is convenient and flexible, and data processing is fast enough.

2. deque should be used to insert and remove elements in the sequence header and tail. If you want the container to automatically reduce the memory size when the elements are removed, you should also use deque. In addition, because vector usually uses a memory block to store elements, and deque uses multiple blocks, the latter can contain more elements.

3. If you need to insert, remove, and move elements frequently in the middle section of the container, you can use list. List provides a special member function that transfers elements from container a to container B within a constant time. However, because list does not support random access, if you only need to know the list header but want to access the elements in the list, the performance will be greatly compromised.

Similar to all "node-based" containers, as long as the elements or containers are no longer available, the list will not invalidate the iterator pointing to those elements. Vector will not. Once its capacity is exceeded, all its iterators and pointers. References will become invalid. When you perform the insert or remove operation, some iterators, pointers, and references will also become invalid. As for deque, when its size changes, all iterators, pointers, and references will be invalid.

4. If the container you want is of this nature: if each operation fails, it will be ineffective. You should choose list or associated container.

5. If you often need to search for elements based on a certain criterion, you should use the set or Multiset that sorts the elements by this sort criterion. Remember, theoretically, In order to sort 1000 elements, the logarithm complexity is 10 times better than the linear complexity. In terms of search speed, hash tables are usually 5-10 times faster than Binary Trees. However, the elements in the hash table are not sorted, so if the elements must be sorted, they will not be used.

6. If you want to process key/value pair, use map or multimap.

7. map should be used if an associative array is required.

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

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.