Deque double-end queue container (Object creation, access through arrays and iterators, element assignment, insertion, deletion, etc.), deque queue

Source: Internet
Author: User

Deque double-end queue container (Object creation, access through arrays and iterators, element assignment, insertion, deletion, etc.), deque queue

Deque is very similar to vector. It not only inserts and deletes elements at the end, but also inserts and deletes elements in the header. However, when considering the memory allocation policy and operational performance of container elements, deque has advantages over vector.

Header file

# Include <deque>

Create a deque object

1) deque (); // create a deque object without any elements.

deque<int> d

2) deque (size_typen); // create a deque object with n elements. Each element uses the default value of its type.

Deque <int> d (10); // The deque Object d has 10 elements. The initial value of each element is 0.

3) deque <size_type n, constT & value); // create a deque object with n elements whose initial values are value.

deque<double> d(10,5);

4) deque (const deque &); // copy constructor of deque. Create a New deque object by copying the element value of a deque object.

deque<char> d1(5,’a’);deque<char> d2(d1);

5) deque (constInputIterator first, const InputIterator last, const A & a = ());

// Copy the elements in the iteration interval [first, last) to a newly created deque object. The memory distributor can be default.

// Use the int array iArray to create a deque object dint iArray [] = {1, 2, 4, 5, 6, 7}; deque <int> d (iArray, iArray + 7 );
Initialize assignment

The push_back () function provided by deque can be used to press the new element value at the end, which is often used as the initialization value assignment of the deque container.

Element Traversal

Access deque Elements Using arrays and iterators.

# Include <deque ># include <iostream> using namespace std; int main (void) {deque <int> d; int I; d. push_back (13); d. push_back (32); d. push_back (29); cout <"Access deque element in array mode:" <endl; for (I = 0; I <d. size (); I ++) cout <"d [" <I <"] =" <d [I] <endl; deque <int> :: iterator j, jend; // defines the iterator jend = d. end (); cout <"iterator access deque element:" <endl; for (I = 0, j = d. begin (); j! = Jend; I ++, j ++) {cout <"d [" <I <"] =" <* j <endl ;} return 0 ;}


Element insertion

Because deque uses two iterators to point to the beginning and end of the dual-end queue respectively, deque has the efficient header insertion element function push_front (). Insert () is used to insert data in the middle.

Void push_front (constT &); // insert a header

Iterator insert (iterator pos, const T & x); // insert the new element x before the pos position

# Include <deque ># include <iostream> using namespace std; int main (void) {deque <int> d; d. push_back (6); d. push_back (7); // Insert d in the header. push_front (5); for (int I = 0; I <d. size (); I ++) // print 6 7 cout <d [I] <''; cout <endl; // Insert d in the middle position. insert (d. begin () + 1, 9); // Insert before the first element, that is, 9 6 7for (int j = 0; j <d. size (); j ++) cout <d [j] <''; cout <endl; return 0 ;}


Element Deletion

The deque container provides the pop_front function for deleting the first element, the pop_back function for deleting the last element, the erase function for deleting elements at any position or iteration interval, and the clear function for deleting all elements.

1) void pop_front (); // Delete the first element of deque

2) void pop_back (); // Delete the last element of deque

3) iterator erase (iteratorpos); // Delete the elements pointed to by the pos

4) iterator erase (iterator first, iterator last); // delete all elements pointed to by the iteration interval [first, last)

5) void clear (); // delete all elements

# Include <deque ># include <iostream> using namespace std; int main (void) {deque <int> d; d. push_back (4); d. push_back (5); d. push_back (1); d. push_back (1); d. push_back (1); d. push_back (6); for (int I = 0; I <d. size (); I ++) cout <d [I] <''; cout <endl; // Delete the Element d at the beginning, end, and any position. erase (d. begin () + 1); d. pop_front (); d. pop_back (); for (int j = 0; j <d. size (); j ++) cout <d [j] <''; cout <endl; // delete all elements d. clear (); cout <"execute clear ()" <endl <"clear all deque elements" <endl; return 0 ;}

Other functions are similar to the vector container. We will not repeat them here. For details, refer to the previous article on the Application of vector.


This article is my original, reproduced please indicate the source: http://blog.csdn.net/lsh_2013/article/details/46737877







Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.