C++ std::queue::front() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++98

Definition

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
CallWhat 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 queueOn 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

ParameterDescription
Nonefront() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
T&A reference to the oldest element. On a const queue it is a const T&.
Undefined behaviorIf 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.

C++
#include <iostream>
#include <queue>

int main() {
    std::queue<int> q;
    q.push(30);
    q.push(10);
    q.push(50);

    std::cout << "Front: " << q.front() << '\n';
    std::cout << "Front again: " << q.front() << '\n';
    std::cout << "Size: " << q.size() << '\n';
    return 0;
}

How It Works

  1. The numbers go in as 30, 10, 50. A queue does not sort, so 30 stays at the front because it joined first.
  2. front() only reads the oldest element. The second call gives 30 again, because nothing was removed.
  3. size() is still 3. This proves front() never changes the queue. Only pop() can remove an element.

Example 2 — Change the Front Element Through the Reference

Because front() returns a reference, you can assign to it or keep it in a reference variable and change the value in place.

C++
#include <iostream>
#include <queue>

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

    q.front() = 99;
    std::cout << "Front: " << q.front() << '\n';

    int& first = q.front();
    first++;
    std::cout << "After increment: " << q.front() << '\n';
    std::cout << "Back: " << q.back() << '\n';
    return 0;
}

How It Works

  1. q.front() = 99; writes straight into the oldest element, so 10 becomes 99. The queue keeps the same size and the same order.
  2. int& first is another name for that same element. first++ raises it from 99 to 100, and front() now shows 100.
  3. 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;
}

How It Works

  1. The queue starts empty, so empty() is true and the program skips front(). This avoids undefined behavior.
  2. The loop condition !jobs.empty() guards every call to front(). The string is copied into job before the queue changes.
  3. 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.

Next: pop()

Remove the front element. It does not return the value with std::queue.

pop() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful