C++ std::deque begin() Function

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

Definition

In C++, std::deque::begin() returns an iterator that points to the first element of the deque. An iterator works like a pointer: *it reads the element it points to.

You usually use it together with end() in a loop, or pass the pair to algorithms such as std::sort. 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 begin with empty brackets. It takes no arguments.

C++
auto it = d.begin();
*it = 100;
CallWhat it does
d.begin()Returns an iterator to the first element.
*d.begin()Reads the first element through the iterator. You can also assign to it.

If you only want to read, use cbegin(). It always gives a read-only iterator.

Library declarations
C++
iterator       begin() noexcept;
const_iterator begin() const noexcept;

Parameters

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

Return Value

Return ValueDescription
IteratorA random-access iterator to the first element. On a const deque it is a const_iterator.
Empty dequeFor an empty deque, begin() is equal to end(), and you must not dereference it.

C++ Examples

Example 1 — Loop with begin()

Walk through every element with an iterator.

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

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

    for (auto it = d.begin(); it != d.end(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. auto it = d.begin() starts the iterator on the first element.
  2. *it reads the current value, and ++it moves to the next one.
  3. The loop stops when it reaches d.end().

Example 2 — Change Values Through the Iterator

A non-const iterator lets you write to the element. A deque iterator also supports +.

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

int main() {
    std::deque<int> d = {1, 20, 30};

    auto it = d.begin();
    *it = 100;
    *(it + 1) += 5;

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

How It Works

  1. *it = 100 replaces the first element, which was 1.
  2. it + 1 jumps straight to the second element. Deque iterators are random-access, so this is allowed.
  3. += 5 adds 5 to 20, so it becomes 25. The range-based loop prints the final values.

Example 3 — Sort with begin() and end()

Algorithms take a pair of iterators. Here std::sort sorts the whole deque.

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

int main() {
    std::deque<int> d = {9, 2, 7, 4, 5};

    std::sort(d.begin(), d.end());

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

How It Works

  1. std::sort needs to know where the range starts and where it stops.
  2. d.begin() is the start, and d.end() is the stop mark. A deque has random-access iterators, so std::sort works on it (a std::list cannot use it).
  3. The values inside the same deque are rearranged in place, so it now reads 2 4 5 7 9.

Frequently Asked Questions

It returns an iterator that points to the first element of the deque. For a const deque it returns a const_iterator.
front() returns the first element itself. begin() returns an iterator that points to it. You write *d.begin() to get the same value as d.front().
Yes. It returns a valid iterator that equals end(). You must not dereference it, because there is no element.
Yes, push_front() and push_back() invalidate all iterators, but references to existing elements stay valid. The simple rule: get a new iterator after you add or remove elements.
For a plain loop over every element, a range-based for loop is simpler. Use begin() when you need an algorithm such as std::sort, or when you need to move the iterator yourself.

Next: cbegin()

Get a read-only iterator to the first element with std::deque.

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

11 people found this page helpful