C + + Standard Template Library STL queue use method and application introduction

Source: Internet
Author: User

C + + Standard Template Library STL queue use method and application introduction

Queue

The queue template class is defined in the <queue> header file.

Similar to the Stack template class, the queue template class requires two template parameters, one element type, one container type, the element type is necessary, the container type is optional, and the default is the Deque type.

The sample code that defines the queue object is as follows:

Queue<int> Q1;

Queue<double> Q2;

The basic operations of the queue are:

Queue, as in example: Q.push (x); Connect X to the end of the queue.

Out of the team, as in Example: Q.pop (); The first element of the popup queue, note that the value of the popup element is not returned.

Access the first element of the team, such as: Q.front (), which is the first element to be pressed into the queue.

Access the tail element, such as: Q.back (), which is the element that was last pressed into the queue.

Determine if the queue is empty, as in example: Q.empty (), returns True when the queue is empty.

Access to the number of elements in the queue, as in example: Q.size ()

Std::queueFIFO queue

The queues is a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), Where elements is inserted into one end of the container and extracted from the other.

QueueS is implemented as containers adaptors, which is classes that use an encapsulated object of a SP Ecific Container class as its underlying container, providing a specific set of member functions to access its EL Ements. Elements is pushed into the ' back ' of the specific container and popped from its ' front ' .

The underlying container may is one of the standard container class template or some other specifically designed container Class. The only requirement are that it supports the following operations:

    • Front ()
    • Back ()
    • Push_back ()
    • Pop_front ()


Therefore, the standard container class templates deque and list can be used. By default, if no container class was specified for a particular queue class, the standard container class Templat E deque is used.

In their implementation in the C + + Standard template Library, queues take the template parameters:

 
templateclassclassclass queue;


Where The template parameters has the following meanings:

    • T: Type of the elements.
    • Container: Type of the underlying container object used to store and access the elements.

In the reference for the queue member functions, these same names is assumed for the template parameters.

C + + Standard Template Library STL queue use method and application introduction

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.