STL Standard Library of C + + learning notes (eight) queue container adapter

Source: Internet
Author: User
Tags pop value

Instead of directly maintaining the controlled sequence, the container adapter implements all functions through the underlying container object stored in it.

The queue class allows you to insert elements at the end of the underlying data structure, and also allows you to insert elements from the front (first in, first out).

Include header file #include <queue> Declaration namespace using namespace Std;

Common Queue operations:

1.void push (const t& val); Inserts an element at the end of the queue (implemented by invoking the Push_back function of the underlying container)

2.void pop (); Deleting elements in front of the queue (implemented by invoking the Pop_back function of the underlying container)

3.value_type& Front (); Gets a reference to the first element in the queue (implemented by invoking the front function of the underlying container)

4.value_type& back (); Gets the last element of the queue (implemented by invoking the back function of the underlying container)

5.bool empty () const; Determines whether the queue is empty (implemented by calling the empty function of the underlying container)

6.size_type size () const; Gets the number of queue elements (implemented by invoking the size function of the underlying container)

For best performance, use the Deque class as the underlying container for the queue

such as: queue<int> values;
Values.push (1);
Values.push (2);
Values.push (3);
while (!values.empty ())
{
cout<< "Pop Value:" <<values.front () <<endl;
Values.pop ();
}

Program output:

STL Standard Library of C + + learning notes (eight) queue container adapter

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.