Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C++ Tutorial

C++ HOME C++ Intro C++ Get Started C++ Syntax C++ Output C++ Comments C++ Variables C++ User Input C++ Data Types C++ Operators C++ Strings C++ Math C++ Booleans C++ If...Else C++ Switch C++ While Loop C++ For Loop C++ Break/Continue C++ Arrays C++ Structures C++ Enums C++ References C++ Pointers

C++ Functions

C++ Functions C++ Function Parameters C++ Function Overloading C++ Scope C++ Recursion

C++ Classes

C++ OOP C++ Classes/Objects C++ Class Methods C++ Constructors C++ Access Specifiers C++ Encapsulation C++ Inheritance C++ Polymorphism C++ Files C++ Exceptions C++ Date

C++ Data Structures

C++ Data Structures & STL C++ Vectors C++ List C++ Stacks C++ Queues C++ Deque C++ Sets C++ Maps C++ Iterators C++ Algorithms

C++ How To

C++ Add Two Numbers C++ Random Numbers

C++ Reference

C++ Reference C++ Keywords C++ <iostream> C++ <fstream> C++ <cmath> C++ <string> C++ <cstring> C++ <ctime> C++ <vector> C++ <algorithm>

C++ Examples

C++ Examples C++ Real-Life Examples C++ Compiler C++ Exercises C++ Quiz C++ Certificate


C++ Queues


C++ Queue

A queue stores multiple elements in a specific order, called FIFO.

FIFO stands for First in, First Out. To visualize FIFO, think of a queue as people standing in line in a supermarket. The first person to stand in line is also the first who can pay and leave the supermarket. This way of organizing elements is called FIFO in computer science and programming.

Unlike vectors, elements in the queue are not accessed by index numbers. Since queue elements are added at the end and removed from the front, you can only access an element at the front or the back.

To use a queue, you have to include the <queue> header file:

// Include the queue library
#include <queue>

Create a Queue

To create a queue, use the queue keyword, and specify the type of values it should store within angle brackets <> and then the name of the queue, like: queue<type> queueName.

// Create a queue of strings called cars
queue<string> cars;

Note: The type of the queue (string in our example) cannot be changed after its been declared.

Note: You cannot add elements to the queue at the time of declaration, like you can with vectors:

queue<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};

Add Elements

To add elements to the queue, you can use the .push() function after declaring the queue.

The .push() function adds an element at the end of the queue:

Example

// Create a queue of strings
queue<string> cars;

// Add elements to the queue
cars.push("Volvo");
cars.push("BMW");
cars.push("Ford");
cars.push("Mazda");

The queue will look like this:

Volvo (front (first) element)
BMW
Ford
Mazda (back (last) element)


Access Queue Elements

You cannot access queue elements by referring to index numbers, like you would with arrays and vectors.

In a queue, you can only access the element at the front or the back, using .front() and .back() respectively:

Example

// Access the front element (first and oldest)
cout << cars.front();  // Outputs "Volvo"

// Access the back element (last and newest)
cout << cars.back();  // Outputs "Mazda"
Try it Yourself »

Change Front and Back Elements

You can also use .front and .back to change the value of the front and back elements:

Example

// Change the value of the front element
cars.front() = "Tesla";

// Change the value of the back element
cars.back() = "VW";

// Access the front element
cout << cars.front();  // Now outputs "Tesla" instead of "Volvo"

// Access the back element
cout << cars.back();  // Now outputs "VW" instead of "Mazda"
Try it Yourself »

Remove Elements

You can use the .pop() function to remove an element from the queue.

This will remove the front element (the first and oldest element that was added to the queue):

Example

// Create a queue of strings
queue<string> cars;

// Add elements to the queue
cars.push("Volvo");
cars.push("BMW");
cars.push("Ford");
cars.push("Mazda");

// Remove the front element (Volvo)
cars.pop();

// Access the front element (Now BMW)
cout << cars.front();
Try it Yourself »

Get the Size of a Queue

To find out how many elements there are in a queue, use the .size() function:

Example

cout << cars.size();
Try it Yourself »

Check if the Queue is Empty

Use the .empty() function to find out if the queue is empty or not.

The .empty() function returns 1 (true) if the queue is empty and 0 (false) otherwise:

Example

queue<string> cars;
cout << cars.empty(); // Outputs 1 (The queue is empty)
Try it Yourself »

Example

queue<string> cars;

cars.push("Volvo");
cars.push("BMW");
cars.push("Ford");
cars.push("Mazda");

cout << cars.empty();  // Outputs 0 (not empty)
Try it Yourself »

Stacks and Queues

Queues are often mentioned together with Stacks, which is a similar data structure described in the previous page.




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.