Skip to content
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
af393a2
[Edit] Python: Python CLI arguments
mamtawardhani Sep 25, 2025
17d337a
Update command-line-arguments.md
mamtawardhani Sep 25, 2025
06bfd59
Merge branch 'Codecademy:main' into main
mamtawardhani Oct 4, 2025
e4aa0b1
Merge branch 'Codecademy:main' into main
mamtawardhani Nov 3, 2025
a25d227
Merge branch 'Codecademy:main' into main
mamtawardhani Nov 5, 2025
10578df
[Term Entry] PyTorch Tensor Operations: .log2()
mamtawardhani Nov 5, 2025
44eedef
Merge branch 'Codecademy:main' into main
mamtawardhani Nov 8, 2025
d23c8e3
Merge branch 'Codecademy:main' into main
mamtawardhani Nov 10, 2025
8690381
Merge branch 'Codecademy:main' into main
mamtawardhani Nov 15, 2025
a8996b9
Merge branch 'Codecademy:main' into main
mamtawardhani Nov 17, 2025
f6a55c2
Merge branch 'Codecademy:main' into main
mamtawardhani Nov 19, 2025
249f619
Merge branch 'Codecademy:main' into main
mamtawardhani Nov 19, 2025
0b94a22
Merge branch 'Codecademy:main' into main
mamtawardhani Nov 26, 2025
fb43261
Merge branch 'Codecademy:main' into main
mamtawardhani Dec 10, 2025
0fbbc39
[Term Entry] C++ Unordered-sets : cend()
mamtawardhani Dec 13, 2025
5a74b04
Delete docs/content/pytorch/concepts/tensor-operations/terms/log2/log…
mamtawardhani Dec 13, 2025
89e84d9
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
8db31c6
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
284797d
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
1921259
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
9e3a1d4
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
d058c55
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
7b5631d
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
80f2d62
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
c76fb1b
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
9d95727
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
bdaeee6
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
08cb53f
Apply suggestion from @avdhoottt
avdhoottt Jan 25, 2026
2b0d29e
Merge branch 'main' into cend
avdhoottt Jan 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions content/cpp/concepts/unordered-set/terms/cend/cend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
---
Title: 'cend()'
Description: 'Returns a constant iterator pointing just past the last element of the unordered set.'
Subjects:
- 'Computer Science'
- 'Game Development'
Tags:
- 'Functions'
- 'Iterators'
- 'Sets'
- 'STL'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

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()`](https://www.codecademy.com/resources/docs/cpp/unordered-set/cbegin) and `cend()` follows the container’s internal hash-table order.

## Syntax

```pseudo
const_iterator cend() const noexcept;
```

Or, to work with a specific bucket:

```pseudo
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:

```cpp
#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:

```shell
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:

```cpp
#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:

```shell
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:

```codebyte/cpp
#include <iostream>
#include <unordered_set>
using namespace std;

int main() {
unordered_set<string> animals = {"cat", "dog", "horse"};

for (auto it = animals.cbegin(); it != animals.cend(); ++it) {
cout << *it << " ";
}

return 0;
}
```


## 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`.
Loading