C + + standard library sequential container

Source: Internet
Author: User

Generic concept.

Sequential containers are basically understood: the elements are stored sequentially, and the ability to provide element-specific operations (iterators or other) and sequential access to elements is provided.

Personal Understanding:

1) Order: Sort by added order of precedence. However, advanced priority or backward priority depends on the specific container.

  Personal application:

1) Use the container as much as possible when you can use the container. Because you can't guarantee that your algorithm is better than the entire development team.

2) Select the basic principle of the container (from c+ primer):

1, use a vector when you do not know how to choose.

2, a single element requires little memory, and when memory is scarce, do not use list/forward_list.

3, when the addition operation is known, try to use a container that conforms to the original design idea. For example: Frequent random insertions/deletions, use list/forward_list, and try not to use Vector,deque.

4, Extrapolate: When mixing add operations, select the most frequent add operation as the criteria for selecting containers. For example: 100 times after adding + one random operation, select Vector,deque. Conversely: List/forward_list.

5, Extrapolate: The add operation is unified at some stage, and container substitution is possible. For example, the INPUT element uses list. After the input is finished, the vector is used to store (copy list) before the read operation.

3) use iterators as much as possible to manipulate containers -although some containers support subscripts. 、

Some details:

1) Containers can hold containers (nested).

Vector container:

Some details:

1) save elements using contiguous memory.

2) You can subscript access to the element (fast). However, it is used as a dynamic array.

3) Intermediate Add/Remove elements are time consuming. The memory needs to be re-adjusted. It also causes the iterator to fail (approximate rate).

4) When adding new elements (push_back), memory may also be reassigned. Causes the iterator to fail (medium probability).

5) container growth mode : when exceeded, increase the memory according to a certain amount to ensure that after adding several elements, it is necessary to allocate memory again. The measure can be set by function (not recommended).

Deque Container:

Some details:

1) save elements using contiguous memory.

2) Not sure if you can use subscript access.

List container:

Some details:

1) the idea of using linked lists.

2) The extra memory overhead is relatively large.

3) subscript is not supported.

Forward_list Container:

Some details:

1) Similar to the list container.

2) Main features: one-way linked list.

Array Container:

Some details:

1) Encapsulation of the array. Enhance security.

2) has the properties of the array.

String container:

Some details:

1) save elements using contiguous memory.

2) to save the string.

Adapter:

Concept: An adapter is a mechanism. The adapter allows the container to mimic other data structures from the behavior.

Sequential containers available Adapters: Stack, queue, priority_queue.

Personal Understanding:

1) is used to simulate some common data structures. As above: Stack, queue.

Doubts:

1) Why not just make a container?

C + + standard library sequential container

Related Article

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.