C + + Common containers

Source: Internet
Author: User

iterators. Basic introduction (from Network): Iterators are an abstract concept. Ability to traverse some/all elements within a container.  Each iterator contains the address of the element. It can combine abstract containers with generic algorithms. Approximate principle: 1)the Iterator object contains the address of the element within the container. 2) through the design, the element operation is unified. Personal Understanding: 1) The concept of iterators, effectively integrating different containers and generic algorithms.  It provides an interface for manipulating elements within a container.  2) Popular Understanding: He is the information interface of STL Library.  3) It requires that all operations should have the same interface. 4) The container needs to provide its own iterator, using the generic algorithm to use the iterator as much as possible. Some details: 1) Changing the storage address of the elements inside the container may cause the original iterator to fail.  Indirectly, the essence of an iterator is a pointer.  2) post-tail iterators usually indicate the end. 3) iterators cannot be obtained using the Fetch address symbol ' & '.  Five kinds of iterators: A true iterator can contain the following types. 1) input/output iterator: Reads the elements in the sequence.  The following points need to be supported: 1,[= =] [!],[+ +],[*],[] 2) forward iterator: one-way move. 3) Bidirectional iterator: Bidirectional move 4) Random access iterator: supports the use of subscript operations.sequential containers.

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 insert/delete, 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.

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: Doubly linked list.

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: Used to simulate some common data structures. As above: Stack, queue.

Wondering: Why not just make a container?

unordered container.

General knowledge:

1) mainly: Map and set. Derived types: Multi and Unorder. For example: Multimap, Unorder_multimap.

2) Use the pair as the unit element.

1,key-value make up a pair.

2,first member: Key. Second member: value.

3,key is immutable for const types.

3) The composition of the associated container: Key-pair.

Basic points:

1) Generic algorithms are not typically used for associative containers.

2) ordered associative containers, with strict weak order requirements. [<=] is used by default. Of course, you can also use predicates or lambda expressions to customize.

C + + Common 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.