C++ Queues

Christine_Yang's avatar
Published May 23, 2022Updated Dec 21, 2022
Contribute to Docs

Queues are container adaptors that store elements in a first-in-first-out (FIFO) order. Elements inserted into the queue first are removed first.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

#include <queue>

queue<type> queueName;

type is the data type that will be stored in the queue.

Codebyte Example

The following codebyte example creates an empty queue, adds elements using the .push() method, and then prints the elements from the queue:

Code
Output

Queues

.back()
Returns a reference to the element at the back of the queue.
.empty()
Checks if a queue has no elements.
.front()
Returns a reference to the element at the front of the queue.
.pop()
Removes the element at the front of the queue.
.push()
Adds an element to the back of the queue.
.size()
Returns the number of elements in the queue.
.swap()
Swaps the values of two queues.
emplace()
Constructs a new element at the end of the queue in-place using forwarded arguments.

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours