C + + Deque (bidirectional queue)

Source: Internet
Author: User

C + + Deque ( bidirectional queuing ) is an optimized basic sequence container for adding and deleting operations on both ends of a sequence. It allows for faster random access, but it does not keep all objects in a contiguous block of memory like a vector, but instead uses multiple contiguous blocks of storage and keeps track of those blocks and their order in a mapping structure. Adding or removing elements to deque is a small cost. It does not need to reallocate space, so adding elements to the end is more efficient than vector.

In fact, Deque is a combination of the pros and cons of vectors and lists, which is a container between the two.

Deque Features of:

(1) Convenient random access, that is, support [] operator and vector.at (), but the performance of the vector is not good;

(2) can be inserted and deleted internally, but the performance is not the list;

(3) can be push, pop at both ends;

(4) Use more memory relative to Verctor.

A bidirectional queue is similar to a vector, but it allows for quick insertion and deletion of the container's head (as in the tail).

1.Constructors Create a new bidirectional queue

Grammar:

Deque ();//Create an empty bidirectional queue

Deque (size_type size);//Create a two-way queue of size

Deque (size_type num, const type &VAL); Place a copy of Num Val into the queue

Deque (const deque &from);//Create a content-like bidirectional queue from the From

Deque (input_iterator start, Input_iterator end);

Start and end-Creates a queue that holds elements from start to end.

2.Operators comparison and assignment bidirectional queue

You can use the [] operator to access a single element in a two-way queue

3.assign () Sets the value of the bidirectional queue

Grammar:

void assign (Input_iterator start, Input_iterator end);

The range indicated by start and end is a two-way queue assignment

void assign (Size num, const TYPE &val);//set to num val.

4.at () returns the specified element
Grammar:

Reference at (size_type POS); Returns a reference to the element on position Pos in a bidirectional queue

5.back () returns the last element

Grammar:

Reference back ();//Returns a reference that points to the last element in a bidirectional queue

6.begin () returns the iterator that points to the first element

Grammar:

Iterator begin ();//Returns an iterator that points to the first element of a bidirectional queue

7.clear () Delete all elements

8.empty () return True if the two-way queue is empty

9.end () returns an iterator pointing to the tail

10.erase () Delete an element

Grammar:

Iterator Erase (iterator POS); Delete an element at the POS location

Iterator Erase (iterator start, iterator end); Delete all elements between start and end

Returns the last element that points to a deleted element

11.front () returns a reference to the first element

12.get_allocator () returns the Configurator for the bidirectional queue

13.insert () inserting an element into a two-way queue

Grammar:

Iterator Insert (iterator pos, size_type num, const type &VAL); Num Val values are inserted in front of POS

void Insert (iterator pos, input_iterator start, Input_iterator end);

Inserting elements from start to end in front of POS

14.max_size () returns the maximum number of elements that a two-way queue can hold

15.pop_back () Delete the trailing element

16.pop_front () Delete the element of the head

17.push_back () Adds an element to the tail

18.push_front () Adds an element to the head

19.rbegin () returns a reverse iterator pointing to the tail

20.rend () returns a reverse iterator pointing to the head

21.resize () Change the size of the two-way queue

22.size () returns the number of elements in a two-way queue

23.swap () and another bidirectional queue interchange element

Grammar:

void swap (deque &target);//swap target and current bidirectional queue elements

C + + Deque (bidirectional queue)

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.