Deque of C ++ STL containers

Source: Internet
Author: User
Deque of C ++ STL containers

Compared with the vector container, deque dual-end queue container has the obvious advantage of efficiently adding and deleting the first and last elements, and is easy to insert elements at any position.
Adding an element at the beginning and end of the deque container will not invalidate any iterator, but deleting an element at the beginning and end will invalidate the iterator pointing to the deleted element, adding or deleting any other location in the deque container will invalidate all iterators of the container.

1. deque create object

(1) deque <int> de; // create an empty deque object


(2) deque <int> de (n); // create a deque object with n elements


(3) deque <int> de (n, value); // create a deque with n elements. The initial value of each element is value.


(4) deque <int> de1 (n, value); deque <int> de2 (D1); // copy each element value of a deque object and create a new deque object


(5) int iarray [] = {1, 2, 3}; deque <int> de (iarray, iarray + 3); // copy iterator range [first, last) to create a new deque object.

2. Common deque Functions

(1) void push_back (const T &) // Insert a new element value at the end of the container


(2) void push_front (const T &) // Insert a new element value at the beginning of the container


(3) void pop_front () deletes the first element of deque


(4) void pop_back () Delete the last element of deque

(5) iterator erase (iterator POS) deletes the elements referred to by the iterator POS

(6) iterator erase (iterator first, iterator last) deletes all elements of the iterator range [first, last)

(7) void clear () call the erase function to clear all elements

(8) reverse_iterator rbegin () // reverse traversal from the end

(9) reverse_iterator rend () // reverse traversal from the header

(10) void swap (deque &) // used for deque exchange

(11) bool empty ()

(12) size_type size ()

(13) size_type max_size ()

(14) Reference Front ()

(15) reference back ()

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.