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;
Call
What 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.
begin() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Iterator
A random-access iterator to the first element. On a const deque it is a const_iterator.
Empty deque
For 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;
}
Output
5 10 15 20
How It Works
auto it = d.begin() starts the iterator on the first element.
*it reads the current value, and ++it moves to the next one.
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;
}
Output
100 25 30
How It Works
*it = 100 replaces the first element, which was 1.
it + 1 jumps straight to the second element. Deque iterators are random-access, so this is allowed.
+= 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;
}
Output
2 4 5 7 9
How It Works
std::sort needs to know where the range starts and where it stops.
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).
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.