Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C++ Squadron queue

Shulou Source: shulou.com Published: 2022-06-01 01:13:05 11月15日 Update

This article mainly explains "how to use queue in C++". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to use queue in C++" together.

I. Definitions

queue is a container converter template that calls #include

< queue>

Queue classes are available.

First, queue initialization

queue ()

Data type must be present during initialization, container can be omitted, default to deque type when omitted

Example of initialization

1:

queueq1; queueq2; queue q3;//Default to queue implemented with deque container;

2:

queue>q1;//queue implemented with list container >q2; //queue implemented with deque container

Note: Cannot initialize queue with vector container

Because queue converters require containers to support front (), back (), push_back (), and pop_front (), this means that queue data is pushed from the back of the container and popped from the front. So queue can be initialized using deque and list, but vector cannot be used for queue because it lacks pop_front ().

II. Queue Common Functions

1. common functions

push() inserts an element at the end of the queue

pop() removes the first element of the queue

size() Returns the number of elements in the queue

empty() Returns true if the queue is empty

front() Returns the first element in the queue

back() Returns the last element in the queue

2. Example of function usage

1: push () insert an element at the end of the queue

queue q; q.push("first"); q.push("second"); cout

Tags: Queues elements containers output clips functions number learning two content common use data examples types converters one line location front end basic operations that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi vpn macOS MySQL