8. Generic programming and C + + Standard Template Library 2.3 Dual-ended queue container

Source: Internet
Author: User

A double-ended queue container is a queue that relaxes access rights. In addition to accessing elements from the header and tail of the queue, the standard double-ended queue also supports direct access by using the subscript operator "[]".

It provides direct access and sequential access methods. Its header file is <deque>.

1) constructors for double-ended queue containers

There are 4 forms of constructors:

Deque ();//Construct a double-ended queue container with size () 0

Deque (size_type n,const t& v=t ());//Initialize a double-ended queue of size n, the second parameter is the initial value of each element, and the object constructed by default is T ()

Deque (const deque& x);//copy constructor, using double-ended queue X to initialize this double-ended queue container

Deque (Const_iterator first,const_iterator last);//Select a section from another container that supports const_iterator to create a new double-ended queue container.

2) using a double-ended queue container

A, access to the double-ended queue container information

For a double-ended queue container,

Size () returns the number of elements in the double-ended queue container;

Max_size () returns the maximum number of elements that can be accommodated in a double-ended queue container;

Empty () returns True when there are no elements in the double-ended queue

B. Placing elements in a double-ended queue container

There are several ways to put elements into a double-ended queue container: constructors, Push_back () methods, insert () methods, [] operators, = operators, swap functions. There is also the Push_front function, whose prototype is:

void Push_front (const t& x);//add element x to the head of the double-ended queue container

C. Remove elements from the double-ended queue container

Using Pop_back (), Pop_front (), Erase (), clear (), you can remove elements from a double-ended queue, and a pop_front () function, compared to a vector container, is a prototype:

void Pop_front ();//Delete the most front-end element in a double-ended queue container

D. Accessing elements in a double-ended queue container

The double-ended queue container can be accessed sequentially or directly. Elements in a double-ended queue container can be combined using Pop_front (), Pop_back (), Front (), back () member functions for sequential access, or you can use iterators to sequentially traverse a double-ended queue. Because of the support for random access iterators, random access is possible.

8. Generic programming and C + + Standard Template Library 2.3 Dual-ended queue container

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.