|
| 1 | +--- |
| 2 | +Title: 'cend()' |
| 3 | +Description: 'Returns a constant iterator pointing just past the last element of the unordered set.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Game Development' |
| 7 | +Tags: |
| 8 | + - 'Functions' |
| 9 | + - 'Iterators' |
| 10 | + - 'Sets' |
| 11 | + - 'STL' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +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. |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +```pseudo |
| 24 | +unordered_set_name.cend(); |
| 25 | +``` |
| 26 | + |
| 27 | +Or, to work with a specific bucket: |
| 28 | + |
| 29 | +```pseudo |
| 30 | +unordered_set_name.cend(n); |
| 31 | +``` |
| 32 | + |
| 33 | +**Parameters:** |
| 34 | + |
| 35 | +- `n` (size_type, optional): The bucket index. Must be less than `bucket_count()`. |
| 36 | + |
| 37 | +**Return value:** |
| 38 | + |
| 39 | +Returns a `const_iterator` pointing to the past-the-end position of the `unordered_set`. |
| 40 | + |
| 41 | +Example 1: Iterating with constant iterators |
| 42 | + |
| 43 | +In this example, `cbegin()` and `cend()` are used to iterate over an `unordered_set` without allowing modification of elements: |
| 44 | + |
| 45 | +```cpp |
| 46 | +#include <iostream> |
| 47 | +#include <unordered_set> |
| 48 | +using namespace std; |
| 49 | + |
| 50 | +int main() { |
| 51 | + unordered_set<int> values = {4, 8, 15, 16, 23}; |
| 52 | + |
| 53 | + for (auto it = values.cbegin(); it != values.cend(); ++it) { |
| 54 | + cout << *it << "\n"; |
| 55 | + } |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +The output of this code is: |
| 62 | + |
| 63 | +```shell |
| 64 | +16 |
| 65 | +15 |
| 66 | +23 |
| 67 | +8 |
| 68 | +4 |
| 69 | +``` |
| 70 | + |
| 71 | +## Example 2: Using `cend()` with a bucket |
| 72 | + |
| 73 | +In this example, `cend(n)` is used to mark the end of iteration for a specific bucket: |
| 74 | + |
| 75 | +```cpp |
| 76 | +#include <iostream> |
| 77 | +#include <unordered_set> |
| 78 | +using namespace std; |
| 79 | + |
| 80 | +int main() { |
| 81 | + unordered_set<int> nums = {1, 2, 3, 4, 5}; |
| 82 | + |
| 83 | + size_t bucket = 0; |
| 84 | + |
| 85 | + for (auto it = nums.cbegin(bucket); it != nums.cend(bucket); ++it) { |
| 86 | + cout << *it << " "; |
| 87 | + } |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +The output of this code is: |
| 94 | + |
| 95 | +```shell |
| 96 | +5 |
| 97 | +``` |
| 98 | + |
| 99 | +## Codebyte Example |
| 100 | + |
| 101 | +In this example, `cend()` is used to safely iterate through an `unordered_set` when only read access is required: |
| 102 | + |
| 103 | +```codebyte/cpp |
| 104 | +#include <iostream> |
| 105 | +#include <unordered_set> |
| 106 | +using namespace std; |
| 107 | +
|
| 108 | +int main() { |
| 109 | + unordered_set<string> animals = {"cat", "dog", "horse"}; |
| 110 | +
|
| 111 | + for (auto it = animals.cbegin(); it != animals.cend(); ++it) { |
| 112 | + cout << *it << " "; |
| 113 | + } |
| 114 | +
|
| 115 | + return 0; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +> **Note:** The output order may vary because unordered_set does not store elements in a defined sequence. |
| 120 | +
|
| 121 | +## Frequently Asked Questions |
| 122 | + |
| 123 | +### 1. What is the end function in unordered set? |
| 124 | + |
| 125 | +The `end()` function returns an iterator pointing to the position just past the last element of an `unordered_set`, marking the end of the container’s range. |
| 126 | + |
| 127 | +### 2. Why use `unordered_set` in C++? |
| 128 | + |
| 129 | +`unordered_set` provides fast average-case lookup, insertion, and deletion by using hash tables instead of ordered structures. |
| 130 | + |
| 131 | +### 3. Difference between end and cend? |
| 132 | + |
| 133 | +`end()` returns a modifiable iterator, while `cend()` returns a constant iterator that does not allow modification of elements. |
0 commit comments