Translation C + + STL container Reference Manual (chapter II <deque>)

Source: Internet
Author: User

Back to General book

In the original section: http://www.cplusplus.com/reference/deque/deque/

1. std::d eque

Template < class T, class Alloc = allocator<t> > class deque;

Dual-ended queues

Deque (pronounced like "deck") is a special abbreviation for Double-ended-queUE. Deque is a dynamic-length sequence container that can be extended to either end or shortened from either end.

Different libraries have different implementations for deque, in general, in the form of a dynamic array. In any case, any element can be accessed through a random iteration accessor, and the storage space of the container is automatically expanded or shrunk as needed.

As a result, Deque provides a vector-like function that is very efficient not only at the end of the container, but also in adding and deleting elements at the front end of the deque. In addition, unlike vectors, deque does not guarantee that all its elements are stored in contiguous spaces: A pointer to an element plus an offset address to access other elements can result in unpredictable results.

Vectors and deque have similar interfaces and can be used for similar purposes, but there is a considerable gap in the internal working mechanism: vectors use an entire array (to store elements) and need to reallocate the entire space when there is an occasional need to expand capacity. While the elements of deque are stored in a variety of different storage spaces, the container maintains the necessary information internally so that any element can be accessed directly through a uniform continuous interface (iterator), and the time required for access is constant. As a result, the internal mechanism of deque is more complex than vector, which makes the deque container more efficient than the vector in some cases, especially when the container stores a long sequence of data, Each reallocation of space (reallocation) will have a lot of overhead.

For those operations that need to insert or delete elements often outside the front and end of the container, the performance of the deque is worse than the list (linked list) and forward list (single-linked list), and the consistency of iterators and references (reference) is lower *.

* Translator: For the time being, I don't understand how this consistency is even lower.

2. Container Properties

Sequence

The elements in the container are arranged in a strictly linear way, and each element can be accessed by its position in the sequence.

Dynamic arrays

Typically, it is implemented in the form of a dynamic array. Allows direct access to any element in the sequence, and adding or removing elements at the front and end of the sequence is relatively faster.

Use of Dispensers

The container uses an allocator (allocator) object to dynamically process storage space.

3. Template parameters

T

The element type, which remains the same as Deque::value_type.

Alloc

The type of the allocator object used to define the storage allocation model *. By default, the template class allocator is used, which defines the simplest storage allocation model and is not dependent on the specific element type.

* Translator: The original word comparison text 邹邹, in fact, the distributor is to achieve dynamic storage space allocation and recovery, of course, the allocation and recycling here is closely related to the specific element type, different types of elements, occupy different space, distribution and recycling operations naturally different.

4. Member types

C++98

Member Type Defined Description
Value_type First template parameter (T)
Allocator_type Second template parameter (Alloc) The default value is:allocator<value_type>
Reference Allocator_type::reference For the default allocator, this type is value_type&
Const_reference Allocator_type::const_reference For the default allocator, this type is const value_type&
Pointer Allocator_type::p ointer For the default allocator, this type is value_type*
Const_pointer Allocator_type::const_pointer For the default allocator, this type is const value_type*
Iterator A value_type random-access iterator can be converted into const iterator
Const_iterator A const value_type random-access iterator
Reverse_iterator Reverse_iterator<iterator> (Translator: Iterator type of reverse iterator)
Const_reverse_iterator Reverse_iterator<const_iterator> (translator: Reverse iterator for read-only iterator type)
Difference_type Signed integral type, with ITERATOR_TRAITS<ITERATOR>: Same as:d Ifference_type It's usually ptrdiff_t.
Size_type Unsigned integer capable of representing any nonnegative difference_type value It's usually size_t.

C++11

Member Type Defined Description
Value_type First template parameter (T)
Allocator_type Second template parameter (Alloc) The default value is:allocator<value_type>
Reference value_type&
Const_reference Const value_type&
Pointer Allocator_traits<allocator_type>::p ointer For the default allocator, this type is value_type*
Const_pointer Allocator_traits<allocator_type>::const_pointer For the default allocator, this type is const value_type*
Iterator A value_type random-access iterator can be converted into const iterator
Const_iterator A const value_type random-access iterator
Reverse_iterator Reverse_iterator<iterator> (Translator: Iterator type of reverse iterator)
Const_reverse_iterator Reverse_iterator<const_iterator> (translator: Reverse iterator for read-only iterator type)
Difference_type Signed integral type, with ITERATOR_TRAITS<ITERATOR>: Same as:d Ifference_type It's usually ptrdiff_t.
Size_type Unsigned integer capable of representing any nonnegative difference_type value It's usually size_t.

5. member functions

constructor function

Construct Deque container (public member function)

Destructors

Deque destructor (public member function)

= operator

Assignment operations (public member functions)

Iterators

Begin

Returns an iterator pointing to the front end of the container (the translator: that is, the first element) (Public member function)

End

Returns an iterator to the end of the container (the translator: A pointer to the last element) (Public member function)

Rbegin

Returns a reverse iterator to the end of the container (translator: This is the first element for a reverse iterator) (Public member function)

Rend

Returns a reverse iterator (public member function) pointing to the front end of the container (translator: This is the last element for a reverse iterator)

Cbegin

Returns a read-only iterator that points to the front end of the container (public member function)

Cend

Returns a read-only iterator that points to the end of the container (public member function)

Crbegin

Returns a read-only reverse iterator that points to the end of the container (public member function)

Crend

Returns a read-only reverse iterator pointing to the front of the container (public member function)

Translator: Here the container front and end refers to the first and last element in the container respectively. The read-only iterator itself can be added or subtracted to point to other elements, but the elements pointed to by the read-only iterator are read-only.

Capacity

Size

Returns the number of elements contained by the container (public member function)

Max_size

Returns the maximum number of elements a container can contain (public member function)

Resize

Change container size (public member function)

Empty

Test whether the container is empty (public member function)

Shrink_to_fit c++11

Delete pre-allocated space that the container has not used * (Public member function)

* Translator: For performance reasons, when a container's pre-allocated space is consumed by newly inserted elements, the container allocates a certain amount of storage space again, so that it does not have to reallocate space each time a new element is inserted. However, pre-allocated space is wasted if it is no longer used. The Shrink_to_fit method frees these unused pre-allocated spaces while preserving the integrity of all elements.

accessing elements

operator []

Access element (public member function)

At

Rhetorical element (public member function)

Front

Access the first element (public member function)

Back

Access the last element (public member function)

Modify

Assign

Assigning content to a container (public member function)

Push_back

Inserting elements at the end of the container (public member functions)

Push_front

Inserting elements at the front of the container (public member functions)

Pop_back

Delete last element (public member function)

Pop_front

Delete first element (Public member function)

Insert

Insert element (public member function)

Erase

Delete element (common member function)

Swap

Exchange content (public member function) (with another container)

Clear

Emptying the contents of a container (public member function)

Emplace c++11

Construct and insert elements (public member functions)

Emplace_front c++11

Constructs and inserts elements at the front of the container (public member functions)

Emplace_back c++11

Constructs and inserts elements at the end of the container (public member functions)

Distributor

Get_allocator

Return allocator * (Public member function)

* Translator: Returns a copy of the current container allocator. Typically, the allocator is called by the container itself, and the user does not need to be concerned. However, when there is a special design need, you can call the method Get_allocator to obtain the allocator object, and then manually call the allocator to allocate and reclaim the storage space of the container.

6. Overloaded non-member functions

Relational operators

Deque relational operators (functions)

Swap

Swapping the contents of two deque containers (template functions)

Translation C + + STL container Reference Manual (chapter II <deque>)

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.