In C++, pop()removes the oldest element from the front of a std::queue. This is the dequeue step: the person at the front of the line leaves.
pop()removes but does not return the value. That is why the usual pattern has two steps: read with front() first, then call pop() to throw the element away.
This function is a member function of std::queue<T>, declared in the <queue> header. It has been available since the first C++ standard, so every compiler supports it. You call it with a dot, for example q.pop(...).
Syntax
Call pop() on the queue. It takes no arguments.
C++
std::queue<int> q;
q.push(5);
q.push(6);
int oldest = q.front(); // read first: 5
q.pop(); // then remove it
// front() is now 6
Call
What it does
q.pop()
Removes the front element. The next oldest element becomes the new front. Nothing is returned.
Each pop() takes constant time. Remember the two-step rule: pop() does not return the removed value, so call front()beforepop() if you still need it. This design keeps the function safe when copying the value might fail.
Library declarations
C++
void pop();
// queue<T, Container> from <queue>
// It calls c.pop_front() on the underlying container.
// The return type is void: the removed value is not given back.
Parameters
Parameter
Description
None
pop() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Nothing
pop() returns void. The queue shrinks by one, and the old front value is gone.
Undefined behavior
If the queue is empty there is nothing to remove. Check empty() first.
C++ Examples
Example 1 — Remove the Oldest Element
Push three numbers, then pop once. The next number moves up to the front.
decltype(q.pop()) is the type that a call to pop() would produce, without calling it. std::is_void_v checks that this type is void, so the output is true.
Because nothing comes back, the program copies q.front() into the variable gotbefore it removes the element.
The second step, q.pop(), deletes the front value 5. The copy in got is safe, and two numbers are left in the queue.
Example 3 — Pop Only When the Queue Has Elements
Run more pops than there are elements, and use empty() to skip the pops that would be undefined behavior.
Popped 1
Popped 2
Queue is empty, skip pop (round 3)
Queue is empty, skip pop (round 4)
How It Works
The loop tries four pops, but the queue only holds two numbers. The check q.empty() runs at the start of every round.
In rounds 1 and 2 the queue is not empty. The program prints front() and then calls pop(), so 1 and 2 are removed in arrival order.
In rounds 3 and 4 empty() is true, so the else branch is skipped. Without this guard, the extra pop() calls would be undefined behavior.
Frequently Asked Questions
It removes the front element, the oldest one in the queue. The next oldest element becomes the new front. It does not return the removed value.
No. pop() returns void. Read the value with front() first, store it in a variable, and then call pop().
It is undefined behavior. The program may crash or corrupt data. Always check that !q.empty() before calling pop().
No. A queue only removes from the front. To remove from both ends, use std::deque with pop_front() and pop_back().
queue::pop() removes the oldest element (first in, first out). priority_queue::pop() removes the highest-priority element, no matter when it was added.