C++ std::deque front() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::deque

Definition

In C++, std::deque::front() returns the first element of a std::deque. It is the same as d[0], but it reads better.

It returns a reference, so you can read the value or change it. The deque must have at least one element. Include <deque> to use it.

This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.

Syntax

Call front with empty brackets. It takes no arguments.

C++
d.front();
d.front() = 1;
CallWhat it does
d.front()Reads the first element.
d.front() = 1Replaces the first element with a new value.

The last element has a matching function: back(). To remove the first element, use pop_front().

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

Parameters

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

Return Value

Return ValueDescription
ReferenceA reference to the first element, which is the same as d[0] and *d.begin().

C++ Examples

Example 1 — Basic front()

Print the first element of a small deque.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {5, 10, 15};

    std::cout << "First element: " << d.front() << '\n';
    return 0;
}

How It Works

  1. The deque holds three numbers, and 5 was stored first.
  2. front() returns a reference to that first slot.
  3. cout prints the value, so the output is 5.

Example 2 — Change the First Element

Assign to front() to replace the first value.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {5, 10, 15};

    d.front() = 1;

    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. front() is a reference, so it can sit on the left side of =.
  2. Only the first slot changes from 5 to 1. The rest of the deque stays the same.
  3. The loop prints all elements, so you can see the new first value.

Example 3 — front() with pop_front()

Serve people in the order they arrived: look at the front, then remove it.

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

int main() {
    std::deque<std::string> line = {"Ann", "Bob", "Cy"};

    while (!line.empty()) {
        std::cout << "Next in line: " << line.front() << '\n';
        line.pop_front();
    }
    return 0;
}

How It Works

  1. The first person to arrive, Ann, is at the front of the deque.
  2. front() reads that name, and pop_front() removes it, so Bob becomes the new front.
  3. The loop repeats until the line is empty. This first-in, first-out order is how a queue works.

Frequently Asked Questions

It returns a reference to the first element of the deque. That is the element at index 0.
No. Calling front() on an empty deque is undefined behavior. Check empty() first.
front() returns the first element itself. begin() returns an iterator that points to it. *d.begin() gives the same value as d.front().
No. front() only looks at it. Use pop_front() to remove it. pop_front() does not return the value, so call front() first.
Yes. The difference is that a deque can also add and remove at the front quickly with push_front() and pop_front(). A vector cannot.

Next: get_allocator()

Get a copy of the allocator the deque uses with std::deque.

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

14 people found this page helpful