In C++, std::deque::end() returns an iterator that points to the place after the last element. It is an end marker, not a real element, so you must never read through it.
It is the partner of begin(): the pair [begin, end) describes the whole deque. 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 end with empty brackets. It takes no arguments.
C++
auto stop = d.end();
d.insert(d.end(), 4); // add at the back
Call
What it does
d.end()
Returns an iterator one place after the last element.
d.insert(d.end(), 4)
Uses the end marker as the insert position, so 4 goes to the back.
If you only want to read, use cend(). To get the last element itself, use back().
end() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Iterator
A random-access iterator that points past the last element. On a const deque it is a const_iterator.
Empty deque
For an empty deque, end() equals begin().
C++ Examples
Example 1 — Loop Until end()
The loop runs while the iterator is not equal to end().
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {2, 4, 6};
for (auto it = d.begin(); it != d.end(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
return 0;
}
Output
2 4 6
How It Works
it starts at begin(), the element 2.
After 6, ++it makes the iterator equal to end().
The loop condition it != d.end() is now false, so the loop stops before reading anything invalid.
Example 2 — end() as an Insert Position
Passing end() to insert() adds the value at the back.
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2, 3};
d.insert(d.end(), 4);
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 2 3 4
How It Works
insert(pos, value) puts the new value beforepos.
Before end() means after the last real element, so 4 goes to the back.
This gives the same result as d.push_back(4).
Example 3 — Compare find() Results with end()
Algorithms use end() to say nothing found.
C++
#include <algorithm>
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {4, 8, 15};
auto a = std::find(d.begin(), d.end(), 16);
auto b = std::find(d.begin(), d.end(), 8);
if (a == d.end()) {
std::cout << "16 not found\n";
}
if (b != d.end()) {
std::cout << "8 found at index " << (b - d.begin()) << '\n';
}
return 0;
}
Output
16 not found
8 found at index 1
How It Works
std::find returns an iterator to the match, or end() if there is no match.
16 is not in the deque, so a == d.end() and the first message prints.
8 is found, so b points to it. b - d.begin() turns the iterator into the index 1.
Frequently Asked Questions
It returns an iterator that points one place after the last element of the deque. It is an end marker, not a real element.
No. It does not point to an element. Reading *d.end() is undefined behavior.
back() returns the last element itself. end() returns an iterator that points after it. The last element is *(d.end() - 1), which is the same as d.back().
Yes. For an empty deque, begin() == end(), so a loop between them runs zero times.
No. push_back() and push_front() invalidate all iterators, including end(). Call end() again after changing the deque.