C + + standard library--sequential container

Source: Internet
Author: User

A container is a collection of objects that hold a particular type. Sequential containers aggregate a single type of element and store and access those elements based on location. The order of elements in a sequential container is independent of the element value, but is determined by the order in which the element values are added to the container.

There are three sequential containers in the standard library, namely Vector,list and deque. Vectors support random, fast access, because the data stored in the vector is stored in memory continuously. In the actual implementation, the vector will request a larger memory space in advance (if the size is specified, typically twice times the specified size), so that each time a new element is added, no need to request new memory space, improve efficiency. The list container is similar to the way a linked list is stored, so inserting elements is more efficient and finding elements inefficient because you have to find them from the beginning. The deque is a dual-ended queue that also supports fast access, while having high efficiency when plugged at both ends. In addition to the three sequential containers, there are three kinds of adapters, namely Stack,queue,priority_queue. Adapters provide higher-level data structures based on the behavior of the underlying sequential container, stacks, queues, and prioritized queues. Containers are class templates that support generic programming and provide support for a variety of data types.

A container has several ways of initializing, including a copy that initializes to another container of the same type (the container type needs to be the same as the container element type), specifies the container size and content initialization, initializes a copy of the element, and so on. A copy that is initialized to a section of an element can be initialized with another container of a different container type, but with a container element type compatible. The container has a restriction on the element type and must be assignable and duplicated. Because the reference assignment differs from the general assignment meaning, the reference cannot be used as a container element. An IO object cannot be assigned and copied, nor can it be used as a container object. There are iterators in the container, different types of operations that can be supported by iterators in different containers, and general iterators support self-increment self-reduction references to determine equality (= = and!). =), the vector and deque iterators support the addition and subtraction of N, the addition and subtraction of iterators, and the comparison of such operations. An iterator is typically a left-closed interval that represents the object to which the left iterator uses its point, and the right iterator uses its previous object. Deleting an add element in a container can cause an iterator to fail, so do not cache (value in a variable) iterator, which is related to how the container is implemented, and the iterator implementation is closely associated with the pointer.

Several types of requirements are defined in the container, often size_type,iterator,const_iterator, and so on. In addition, the container provides a variety of operations, including inserting data: Push_back (), Push_front (), insert (), and so on. Only list and deque support Push_front () operation, Vector is not supported. There are several ways to invoke an insert operation. The container provides support for relational operators, but the specific operation depends on the container element type. Other operations such as delete, define size, assign value, etc. are supported. The vector container is a special implementation, providing capacity and reserve functions, capacity returns the maximum number of data elements the container can hold, and reserve can specify the number of elements that can be stored. The specific container selection can be determined according to the needs of the application, but vectors are generally used. Because of its special realization way to ensure its high efficiency. A string object can be seen as a character container that supports many of the operations supported by sequential containers, while providing many of its own unique interfaces.

C + + standard library--sequential container

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.