Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::queue — FIFO queue

std::queue is a container adaptor implementing FIFO order: the first element inserted is the first removed. It supports insertion at the back, access to both ends, and removal from the front, without a public iterator interface.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <queue>
std::queue<T> q;
std::queue<T, Container> custom;
q.push(value);
q.emplace(args...);
q.front();
q.back();
q.pop();
q.empty();
q.size();

std::queue<T, Container> from <queue> provides a FIFO (first in, first out) queue. Its default container is std::deque<T>. A custom container must satisfy the adaptor's requirements, including support for front(), back(), push_back() and pop_front(); std::list<T> is one example.

push(value) adds an element at the back, while emplace(args...) constructs it there from the supplied arguments. front() and back() return references to the first and last elements. pop() removes the first element and returns void: to retain its value, copy or move it before removal. Before calling front(), back() or pop(), ensure the queue is not empty; empty() tests emptiness, and size() reports the element count.

The adaptor exposes neither public iterators nor indexing. The example prints elements in insertion order and empties the queue. The code targets compilation with the C++23 flag; production runtime compatibility is not attested. The linked document is an evolving working draft, not a frozen C++23 specification.

Examples

#include <iostream>
#include <queue>

int main() {
    std::queue<int> tasks;
    tasks.push(10);
    tasks.emplace(20);
    tasks.push(30);

    while (!tasks.empty()) {
        std::cout << tasks.front() << '\n';
        tasks.pop();
    }
}

Result

10
20
30

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources