C++ cend()

MamtaWardhani's avatar
Published Jan 26, 2026
Contribute to Docs

The cend() method returns a constant iterator that points to the past-the-end position of an unordered_set. This iterator marks the end of the container’s range and cannot be dereferenced.

Because unordered_set does not maintain a defined order, iteration using cbegin() and cend() follows the container’s internal hash-table order.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

const_iterator cend() const noexcept;

Or, to work with a specific bucket:

const_local_iterator cend(size_type n) const;

Parameters:

  • n (size_type, optional): The bucket index. Must be less than bucket_count().

Return value:

  • No-parameter version: Returns a const_iterator pointing to the past-the-end position of the entire unordered_set.
  • Bucket version: Returns a const_local_iterator pointing to the past-the-end position of bucket n.

Example 1: Iterating with constant iterators

In this example, cbegin() and cend() are used to iterate over an unordered_set without allowing modification of elements:

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_set<int> values = {4, 8, 15, 16, 23};
for (auto it = values.cbegin(); it != values.cend(); ++it) {
cout << *it << "\n";
}
return 0;
}

The output of this code is:

16
15
23
8
4

Note: For all code examples, the output order is implementation-dependent and may vary between different compilers, runs, or systems due to internal hash table organization.

Example 2: Using cend() with a bucket

In this example, cend(n) is used to mark the end of iteration for a specific bucket:

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_set<int> nums = {1, 2, 3, 4, 5};
// Find which bucket contains the element 3
size_t bucket = nums.bucket(3);
cout << "Elements in bucket " << bucket << ": ";
for (auto it = nums.cbegin(bucket); it != nums.cend(bucket); ++it) {
cout << *it << " ";
}
return 0;
}

The output of this code is:

Elements in bucket 3: 3

Codebyte Example

In this example, cend() is used to safely iterate through an unordered_set when only read access is required:

Code
Output

Frequently Asked Questions

1. What is the cend() function in unordered_set?

The cend() function returns a constant iterator pointing to the position just past the last element of an unordered_set, marking the end of the container’s range.

2. Why use unordered_set in C++?

unordered_set provides fast average-case lookup, insertion, and deletion by using hash tables instead of ordered structures.

3. Difference between end and cend?

Both end() and cend() return iterators that cannot modify elements (all iterators in unordered_set point to const elements). The difference is that end() returns an iterator or const_iterator depending on whether the container is const, while cend() always returns const_iterator.

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours