|
| 1 | +Title: 'end()' |
| 2 | +Description: 'Returns an iterator that points to the past-the-end position of the unordered set or the end position in a specific bucket.' |
| 3 | +Subjects: |
| 4 | + - 'Code Foundations' |
| 5 | + - 'Computer Science' |
| 6 | +Tags: |
| 7 | + - 'Iterators' |
| 8 | + - 'Sets' |
| 9 | + - 'STL' |
| 10 | +CatalogContent: |
| 11 | + - 'learn-c-plus-plus' |
| 12 | + - 'paths/computer-science' |
| 13 | +--- |
| 14 | + |
| 15 | +The **`end()`** method returns an iterator that points to the past-the-end position of an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). This iterator marks one past the last element and cannot be dereferenced. Because `unordered_set` does not maintain a defined order, iteration proceeds in the container’s internal hash-table order. |
| 16 | + |
| 17 | +## Syntax |
| 18 | + |
| 19 | +```pseudo |
| 20 | +unordered_set_name.end(); |
| 21 | +``` |
| 22 | + |
| 23 | +**Return value:** |
| 24 | + |
| 25 | +Returns an `iterator` pointing to the past-the-end position of the `unordered_set`. |
| 26 | + |
| 27 | +Or, to work with a specific bucket: |
| 28 | + |
| 29 | +```pseudo |
| 30 | +unordered_set_name.end(n); |
| 31 | +``` |
| 32 | + |
| 33 | +**Parameters:** |
| 34 | + |
| 35 | +- `n` (size_type): The bucket index. Must be less than `bucket_count()`. |
| 36 | + |
| 37 | +**Return value:** |
| 38 | + |
| 39 | +A `local_iterator` pointing to the past-the-end position in bucket `n`. If the bucket is empty, `begin(n) == end(n)`. |
| 40 | + |
| 41 | +## Example |
| 42 | + |
| 43 | +This example shows iterating over an `unordered_set` using `begin()` and `end()`: |
| 44 | + |
| 45 | +```cpp |
| 46 | +#include <iostream> |
| 47 | +#include <unordered_set> |
| 48 | +using namespace std; |
| 49 | + |
| 50 | +int main() { |
| 51 | + unordered_set<int> unique_numbers = {10, 5, 20, 15}; |
| 52 | + |
| 53 | + for (auto it = unique_numbers.begin(); it != unique_numbers.end(); ++it) { |
| 54 | + cout << *it << "\n"; |
| 55 | + } |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +A sample output might be: |
| 62 | + |
| 63 | +```shell |
| 64 | +20 |
| 65 | +5 |
| 66 | +10 |
| 67 | +15 |
| 68 | +``` |
| 69 | + |
| 70 | +> **Note:** The iterator returned by `end()` marks the boundary for iteration and cannot be dereferenced. |
| 71 | +
|
| 72 | +## Codebyte Example |
| 73 | + |
| 74 | +This example retrieves iterators for a specific bucket in the `unordered_set` and prints all elements stored in that bucket: |
| 75 | + |
| 76 | +```codebyte/cpp |
| 77 | +#include <iostream> |
| 78 | +#include <unordered_set> |
| 79 | +using namespace std; |
| 80 | +
|
| 81 | +int main() { |
| 82 | + unordered_set<string> words = {"cat", "dog", "rabbit", "lion"}; |
| 83 | +
|
| 84 | + size_t bucket = 0; |
| 85 | +
|
| 86 | + auto it = words.begin(bucket); |
| 87 | + auto last = words.end(bucket); |
| 88 | +
|
| 89 | + cout << "Elements in bucket " << bucket << ":\n"; |
| 90 | +
|
| 91 | + for (; it != last; ++it) { |
| 92 | + cout << " " << *it << "\n"; |
| 93 | + } |
| 94 | +
|
| 95 | + return 0; |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +A sample output might be: |
| 100 | + |
| 101 | +```shell |
| 102 | +Elements in bucket 0: |
| 103 | + cat |
| 104 | + dog |
| 105 | + lion |
| 106 | +``` |
| 107 | + |
| 108 | +Exact elements shown depend on the hash function and bucket count; your output may differ. |
0 commit comments