In C++, front() lets you look at the oldest element of a std::queue. This is the element that has waited the longest, so it is the next one to leave when you call pop().
front() only reads by default. It does not remove anything, so calling it twice gives the same answer. A queue has no [] and no iterators, so front() and back() are the only two elements you can see.
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.front(...).
Syntax
Call front() on the queue. It takes no arguments.
C++
std::queue<int> q;
q.push(30);
q.push(50);
q.push(10);
int oldest = q.front(); // 30, the first one pushed
Call
What it does
q.front()
Returns a reference to the oldest element. You can read it or change it. The element stays in the queue.
q.front() // const queue
On a const queue it returns a const reference, so you can only read the value.
Unlike priority_queue::top(), the result is a normal reference, so q.front() = 99; works. To remove the element, call pop() afterwards. The usual pattern is read with front(), then remove with pop().
Library declarations
C++
reference front();
const_reference front() const;
// queue<T, Container> from <queue>
// front() returns c.front(), the first element of
// the underlying container (a deque by default).
Parameters
Parameter
Description
None
front() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
T&
A reference to the oldest element. On a const queue it is a const T&.
Undefined behavior
If the queue is empty there is no element to return. Check empty() first.
C++ Examples
Example 1 — Read the Oldest Element
Push three numbers. The first number pushed is always at the front, and reading it does not remove it.
q.front() = 99; writes straight into the oldest element, so 10 becomes 99. The queue keeps the same size and the same order.
int& first is another name for that same element. first++ raises it from 99 to 100, and front() now shows 100.
back() is still 30. Changing the front value never touches the other elements.
Example 3 — Check empty() Before front() (Strings)
A print queue gives jobs out in the order they arrived. Copy the front value first, because pop() returns nothing.
C++
#include <iostream>
#include <queue>
#include <string>
int main() {
std::queue<std::string> jobs;
if (jobs.empty()) {
std::cout << "No jobs yet, so front() is not called.\n";
}
jobs.push("report.pdf");
jobs.push("photo.png");
jobs.push("notes.txt");
while (!jobs.empty()) {
std::string job = jobs.front();
jobs.pop();
std::cout << "Printing " << job << '\n';
}
return 0;
}
Output
No jobs yet, so front() is not called.
Printing report.pdf
Printing photo.png
Printing notes.txt
How It Works
The queue starts empty, so empty() is true and the program skips front(). This avoids undefined behavior.
The loop condition !jobs.empty() guards every call to front(). The string is copied into job before the queue changes.
pop() then removes the front job. The jobs print in arrival order: report.pdf, photo.png, notes.txt.
Frequently Asked Questions
It returns a reference to the oldest element in the queue, the one that will leave next. It does not remove the element.
No. front() only reads the element. Call pop() afterwards to remove it. The usual pattern is to read with front() and then call pop().
It is undefined behavior. The program may crash or return garbage. Always check that !q.empty() before calling front().
Yes. Unlike priority_queue::top(), queue::front() returns a normal reference on a non-const queue, so q.front() = 5; changes the stored element.
front() is the oldest element, which leaves first. back() is the newest element, which leaves last. Both only read (or edit) and neither removes anything.