In C++, std::deque::cend() returns a const_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 cbegin(): together they describe the whole deque for loops and algorithms that only read. 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 cend with empty brackets. It takes no arguments.
C++
auto stop = d.cend();
auto last = d.cend() - 1; // only if d is not empty
Call
What it does
d.cend()
Returns a read-only iterator one place after the last element.
d.cend() - 1
Steps back to the last element. This is safe only when the deque is not empty.
For a normal (changeable) end iterator, use end().
Library declarations
C++
const_iterator cend() const noexcept;
Parameters
Parameter
Description
None
cend() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Const iterator
A const_iterator that points past the last element. It is used only for comparing and stopping loops.
Empty deque
For an empty deque, cend() equals cbegin().
C++ Examples
Example 1 — cend() in a Read-Only Function
A function that takes a const deque can still loop with cbegin() and cend().
C++
#include <deque>
#include <iostream>
void show(const std::deque<int>& values) {
for (auto it = values.cbegin(); it != values.cend(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
}
int main() {
std::deque<int> d = {7, 8, 9};
show(d);
return 0;
}
Output
7 8 9
How It Works
The parameter is const std::deque<int>&, so the function can read the deque but not change it.
The loop starts at cbegin() and keeps going while it != values.cend().
When it moves past 9, it equals cend() and the loop stops.
Example 2 — Reach the Last Element with cend() - 1
Deque iterators are random-access, so you can step backwards from the end marker.
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {10, 20, 30, 40};
auto last = d.cend() - 1;
std::cout << "last element: " << *last << '\n';
return 0;
}
Output
last element: 40
How It Works
d.cend() points one place after 40, which is not a real element.
Subtracting 1 moves the iterator back onto 40, the last real element.
This is only safe because the deque has elements. On an empty deque, cend() - 1 would be invalid.
Example 3 — Count Values with cbegin() and cend()
Pass the pair to an algorithm that only reads the data.
C++
#include <algorithm>
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {2, 5, 2, 8, 2};
int twos = std::count(d.cbegin(), d.cend(), 2);
std::cout << "number of 2s: " << twos << '\n';
return 0;
}
Output
number of 2s: 3
How It Works
std::count checks every element from the first iterator up to, but not including, the second one.
cend() tells it where to stop, so the last element 2 is still counted.
The value 2 appears three times, so the result is 3.
Frequently Asked Questions
It returns a const_iterator that points one place after the last element of the deque. It is an end marker, not a real element.
On a non-const deque, end() returns a normal iterator, and cend() returns a read-only const_iterator. Both point to the same place.
No. It does not point to an element. Reading *d.cend() is undefined behavior.
Yes. For an empty deque, cbegin() == cend(), so a loop between them runs zero times.
Use *(d.cend() - 1) on a non-empty deque. It is usually simpler to call d.back() instead.