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

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

Definition

In C++, back() lets you look at the newest element of a std::queue. This is the element that joined last, so it will be the last one to leave.

back() is the other end of the line from front(). Every push() puts a new element behind the old ones, so after a push() the new value is what back() returns. It does not remove anything.

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.back(...).

Syntax

Call back() on the queue. It takes no arguments.

C++
std::queue<int> q;
q.push(30);
q.push(50);
q.push(10);

int newest = q.back();   // 10, the last one pushed
CallWhat it does
q.back()Returns a reference to the newest element. You can read it or change it. The element stays in the queue.
q.back() // const queueOn a const queue it returns a const reference, so you can only read the value.

There is no function to remove the back element. A queue only removes from the front with pop(). If you need to take items out at the back too, use std::deque and its pop_back().

Library declarations
C++
reference back();
const_reference back() const;

// queue<T, Container> from <queue>
// back() returns c.back(), the last element of
// the underlying container (a deque by default).

Parameters

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

Return Value

Return ValueDescription
T&A reference to the newest 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 Newest Element

Push three numbers, read both ends, then push one more. Only the back changes.

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

int main() {
    std::queue<int> q;
    q.push(7);
    q.push(3);
    q.push(9);

    std::cout << "Front: " << q.front() << '\n';
    std::cout << "Back: " << q.back() << '\n';

    q.push(4);
    std::cout << "Back after push(4): " << q.back() << '\n';
    std::cout << "Front is still: " << q.front() << '\n';
    return 0;
}

How It Works

  1. After three pushes the line is 7, 3, 9. front() is the first value 7 and back() is the last value 9.
  2. push(4) adds a new element behind 9, so back() changes to 4 at once.
  3. The front does not move, because only pop() changes the front. The oldest element 7 is still first in line.

Example 2 — Front and Back Can Be the Same Element

When the queue has just one element, that element is both the oldest and the newest. The two functions return the same object.

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

int main() {
    std::queue<char> q;
    q.push('A');

    std::cout << std::boolalpha;
    std::cout << "Front: " << q.front() << ", Back: " << q.back() << '\n';
    std::cout << "Same element: " << (&q.front() == &q.back()) << '\n';

    q.push('B');
    std::cout << "Front: " << q.front() << ", Back: " << q.back() << '\n';
    std::cout << "Same element: " << (&q.front() == &q.back()) << '\n';
    return 0;
}

How It Works

  1. With only 'A' inside, there is nothing older and nothing newer. Both front() and back() return 'A'.
  2. The & operator takes the address of each returned reference. The addresses match, so it really is the same object, and the program prints true.
  3. After push('B') the two ends are different elements. front() keeps 'A', and back() becomes 'B', so the check prints false.

Example 3 — Change the Back Element Through the Reference

back() returns a reference on a normal queue, so you can edit the newest element in place, for example to add text to the last message.

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

int main() {
    std::queue<std::string> msgs;
    msgs.push("hello");
    msgs.push("world");

    msgs.back() += "!";
    std::cout << "Back: " << msgs.back() << '\n';

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

How It Works

  1. msgs.back() is a reference to the string "world". The += operator appends "!" right inside the queue, with no extra push.
  2. The first print proves the change: back() now returns "world!". The queue still holds two elements.
  3. The loop serves the messages in FIFO order. hello leaves first and the edited world! leaves last.

Frequently Asked Questions

It returns a reference to the newest element in the queue, the one that was pushed last. It does not remove the element.
No. back() only reads (or edits) the newest element. A queue has no function that removes from the back. pop() always removes from the front.
It is undefined behavior. The program may crash or return garbage. Always check that !q.empty() before calling back().
The element you just pushed. Every push() places the new value behind all older values, so back() always returns the most recent push() or emplace().
No. std::priority_queue only offers top() for the highest-priority element. Only std::queue and std::deque have back().

Next: emplace()

Build a new element right inside the queue, at the back with std::queue.

emplace() 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.

7 people found this page helpful