Deque Learning Record of C + + STL

Source: Internet
Author: User

Deque Learning Record of C + + STL

header File

#include <deque>

Brief introduction

See http://www.cplusplus.com/reference/deque/deque/

Double ended queue (bidirectional queues)

deque (usually pronounced like "deck") are an irregular acronym of Double-ended queUE. double-ended queues is sequence containers with the dynamic sizes that can is expanded or contracted on both ends (either its Front or its back).

1. Random access, support [] access and iterator access, this is similar to vector, but performance is not good vector

2. You can insert and delete operations, which is similar to list, but performance is not list good

3. The elements of the deque are not contiguous in memory, but are divided into a number of contiguous memory blocks, which differ from the continuous memory of the vector and the isolated memory of the list, thus showing the above characteristics.

member functions

Constructor (constructor): See http://www.cplusplus.com/reference/deque/deque/deque/

1. Default constructor (Defalult constructor)

Deque (): Creates a new empty bidirectional pair of columns with no elements.

Deque (size_type N): Creates a new two-way queue with a size of n

2. Fill the constructor (fill constructor)

Deque (size_type N, const value_type &val): Creates a new bidirectional queue with a size of n and each element is Val

        Example:deque<int> first (4, 100);//Create a two-way queue named First, size 4, each element is 100

3. Range Constructor (range constructor)

Deque (iterator begin, iterator end): Creates a new bidirectional queue and copies all the elements in the middle of the iterator begin and end.

        Example:deque<int> second (First.begin (), First.end ());//Create a two-way queue named second and copy the first from the beginning to the end of the element to second

4. Copy constructor (copy constructor)

Denque (const deque& x): Constructs an identical two-way queue with two-way queue X

Iterator (iterator) function:

   1. Begin (), End (): iterator Begin ();  iterator End (); Return iterator to Beginning (end);

   2. Rbegin (), rend (): iterator Rbegin ();  iterator rend (); return iterator to reverse beginning (reverse end);

2. Cbegin (), Cend (), Crbegin (), Crend (): iterator Cbegin (); iterator Cend (); iterator Crbegin ();  iterator Crend (); constant

Capacity (capacity) function:

    1. Size (): size_type size (); //return the size of current deque

    2. Max_size (): size_tyep max_size () //return the maximum based on the system and implementation Limitat Ions

    3. Resize (): void Resize (size_type n);

void Resize (size_type n, const value_type &val);        

Resolution: If N is smaller than the current container size, the content was reduced to its first n elements, Remo Ving Tsun

Those beyond (and destroying them). If n is greater than the current container size, the content is expanded

By inserting at the end as many elements as needed to reach a size of n. If Val is specified, the new elements

is initialized as copies of Val, otherwise, they is value-initialized.

    4. Empty (): bool Empty (); Reutrn true if the deque is empty else return false

    5. Shrink_to_fit (): void Shrink_to_fit (); C++11, the deque allocates more memory than its size and it frees the memory that contain no element

Access function (assess elements):

1. operator[]: value_type& operator[] (size_type N); Return a reference of the element in index n which can be changed (assign)

Const value_type& operator[] (size_type n) const; Return a reference of the element in index n which can ' t be write

2. At : value_type& at (size_type N); Similar to the Funciton operator[]

       Const value_type& at (size_type N) const;

3. Front (), Back (): value_type Front (size_type N);

Const Value_type Front (size_type n) const;

Value_type Back (size_type N);

Const Value_type Back (size_type n) const;

 

Modifying a function (modifiers)

    1. Assign (): void assign (iterator begin, iterator end);

void Assign (size_type N, Value_type &val);

    2. Push_back (), Push_front (): void push_back (const value_type& val); Push an element to the deque at back

void Push_front (const value_type& val); Push an element to the deque at front

    3. Pop_back (), Pop_front (): void Pop_back (); Pop an element from the deque on back

void Pop_front (); Pop an element from the deque at front

4. Insert (): iterator Insert (const iterator it, Value_type &val); Insert a value before at specific position

         iterator Insert (const iterator it, Size_type N, Value_type &val); Insert n values before at specific posion

          ITERAOTR Insert (const iterator it, iterator begin, iterator end);

About return value:reutrn a iterator that the newly insert element

    5. Erase (): iterator Erase (const iterator position); Erase the element at specific position

Iterator etase (cosnt iterator begin, iterator end); Erase the element at the range

About the return value:n iterator pointing to the new location of the element that followed the last element erased by T He function call.

6. Swap (): void Swap (deque& x); Swap the element of the Deques

     7. Clear (): void Clear (); Clear the Deque

    

   

  

Deque Learning Record of C + + STL

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.