C++ std::deque cend() Function

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

Definition

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
CallWhat it does
d.cend()Returns a read-only iterator one place after the last element.
d.cend() - 1Steps 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

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

Return Value

Return ValueDescription
Const iteratorA const_iterator that points past the last element. It is used only for comparing and stopping loops.
Empty dequeFor 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;
}

How It Works

  1. The parameter is const std::deque<int>&, so the function can read the deque but not change it.
  2. The loop starts at cbegin() and keeps going while it != values.cend().
  3. 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;
}

How It Works

  1. d.cend() points one place after 40, which is not a real element.
  2. Subtracting 1 moves the iterator back onto 40, the last real element.
  3. 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;
}

How It Works

  1. std::count checks every element from the first iterator up to, but not including, the second one.
  2. cend() tells it where to stop, so the last element 2 is still counted.
  3. 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.

Next: clear()

Remove every element from the deque with std::deque.

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

13 people found this page helpful