Effective STL 1th: Carefully select the container type

Source: Internet
Author: User

C + + offers several different containers for us to choose from, and here's a quick recap:

1. Standard STL sequence container: Vector, String, deque, and list.

2. Standard STL Associative containers: Set, Multiset, map, and Multimap.

3, non-standard sequence container slist and rope. Slist is a one-way linked list, rope is essentially a "heavy" string.

4, non-standard associated containers hash_set, Hash_multiset, Hash_map and Hash_multimap.

5, vector<char> as a string substitution.

6, vector as the standard associated container substitution.

7, several standard non-STL container, including array, Bitset, Valarry, Stack, queue and priority_queue.

Later on, we will discuss the important issues that correspond to the complexity of the algorithm about the container. This first introduces a classification method for STL containers, which is the distinction between contiguous memory containers (Contiguous-memorycontainer) and node-based containers (node-based container) .

A contiguous memory container (or an array-based container, array-based container) stores its elements in one or more blocks (dynamically allocated) memory, with multiple elements in each block of memory. When a new element is inserted or an existing element is deleted, the other elements in the same memory block move forward or backward to make room for the new element, or to fill the void left by the deleted element. This movement affects efficiency and exceptional security. The standard contiguous memory containers are vector, string, and deque. Non-standard rope is also a contiguous memory container.

A node-based container holds only one element in each (dynamically allocated) memory. The insertion or deletion of elements in a container affects only the pointer to the node, not the contents of the node itself, so when there is an insert or delete operation, the value of the element does not need to be moved. Containers that represent lists, such as list and slist, are node-based. The same is true for all standard associative containers (usually implemented as a balanced tree). A non-standard hash container uses a different node-based implementation.

With the above terminology as the basis, the following outlines some of the most important issues when selecting a container.

L Do you need to insert new elements anywhere in the container? Select the Sequence container, if necessary, and the associated container is not possible.

L Do you care if the elements in the container are sorted? If you don't care, the hash container is a viable option; otherwise, you should avoid hashing the container.

Do you choose a container that must be part of standard C + +? if necessary, the hash container, slist, and rope are excluded.

L What type of iterator do you need? if they must be random-access iterators, the selection of the container is limited to vectors, deque, and string. Maybe you can think about rope, too. If you require a bidirectional iterator, you should avoid slist.

When inserting or deleting an element, is it important to avoid moving the original element in the container? If yes, avoid contiguous memory containers.

does the layout of the data in the container need to be compatible with C? if compatibility is required, only vectors can be selected.

is the find speed of the L element a key consideration? If so, hash the containers, the ordered vectors, and the standard associative containers-perhaps this is the order of precedence.

Do you mind if the reference counting technique (reference counting) is used inside the container ? If so, avoid using string because many of the string implementations use reference counts. Rope should also be avoided, because the authoritative rope implementation is based on reference counting. Of course, you need some kind of method for representing strings, so you can consider vector<char>.

for INSERT and delete operations, do you need transactional semantics (transaction semantics)? in other words, when the insert and delete operations fail, do you have the ability to roll back? If necessary, you will use a node-based container. If a transaction is required for the insertion of multiple elements (that is, in the form of an interval), you need to select list, because in a standard container only the list provides transactional semantics for the insert operation of multiple elements. Transactional semantics are especially important for programmers who want to write exception-safe (Exception-safe) code. (a container that uses contiguous memory can also get transactional semantics, but it pays a performance cost and the code is less straightforward.)

do you need to make the least number of iterations, pointers, and references invalid? If this is the case, use a node-based container because the INSERT and delete operations on such containers never make iterators, pointers, and references invalid (unless they point to an element that you are deleting). Insert and delete operations for contiguous memory containers generally make iterators, pointers, and references to the container invalid.

if the iterator for a sequence container is a random access type, and as long as no delete operation occurs, and the insert operation only occurs at the end of the container, then pointers and references to the data will not become invalid are such containers helpful to you? This is a very special situation, but if you're faced with the same situation, then deque is the container you want. (Interestingly, the deque iterator may become invalid when the insert operation occurs only at the end of the container.) Deque is unique, iterators may become invalid, and pointers and references do not become invalid STL standard containers. )

Effective STL 1th: Carefully select the container type

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.