|
| 1 | +--- |
| 2 | +Title: 'cbegin()' |
| 3 | +Description: 'Returns a constant iterator pointing to either the first element of the unordered set or the first element in a specific bucket.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Iterators' |
| 9 | + - 'Sets' |
| 10 | + - 'STL' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-c-plus-plus' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`cbegin()`** method returns a constant iterator that points to the first element of an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). A constant iterator allows read-only access to elements and prevents modification. Because `unordered_set` does not maintain any defined order, the element returned by `cbegin()` depends on its internal hash table structure. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +unordered_set_name.cbegin(n); |
| 22 | +``` |
| 23 | + |
| 24 | +**Return value:** |
| 25 | + |
| 26 | +Returns a `const_iterator` (constant iterator) pointing to the first element in the `unordered_set`. |
| 27 | + |
| 28 | +Or, alternatively: |
| 29 | + |
| 30 | +```pseudo |
| 31 | +unordered_set_name.cbegin(n); |
| 32 | +``` |
| 33 | + |
| 34 | +**Parameters:** |
| 35 | + |
| 36 | +- `n` (size_type): The bucket index. Must be less than `bucket_count()`. |
| 37 | + |
| 38 | +**Return value:** |
| 39 | + |
| 40 | +A `const_local_iterator` pointing to the first element in bucket `n`. If the bucket is empty, the returned iterator equals `cend(n)`. |
| 41 | + |
| 42 | +## Example |
| 43 | + |
| 44 | +This example demonstrates obtaining the starting element of an `unordered_set` using `cbegin()`: |
| 45 | + |
| 46 | +```cpp |
| 47 | +#include <iostream> |
| 48 | +#include <string> |
| 49 | +#include <unordered_set> |
| 50 | +using namespace std; |
| 51 | + |
| 52 | +int main() { |
| 53 | + unordered_set<int> unique_numbers = {10, 5, 20, 15}; |
| 54 | + |
| 55 | + auto it = unique_numbers.cbegin(); |
| 56 | + |
| 57 | + cout << "The first element in internal order is: " << *it << "\n"; |
| 58 | + |
| 59 | + ++it; |
| 60 | + if (it != unique_numbers.cend()) { |
| 61 | + cout << "The second element is: " << *it << "\n"; |
| 62 | + } |
| 63 | + |
| 64 | + // *it = 99; // Error: cannot modify through const_iterator |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +A sample output of this code is: |
| 71 | + |
| 72 | +```shell |
| 73 | +The first element in the set's internal order is: 20 |
| 74 | +The second element is: 5 |
| 75 | +``` |
| 76 | +
|
| 77 | +> **Note:** Attempting to modify the element pointed to by the `const_iterator` would result in a compilation error. |
| 78 | +
|
| 79 | +## Codebyte Example |
| 80 | +
|
| 81 | +In this example, the code retrieves a constant iterator for a specific bucket in the `unordered_set` and prints all elements stored in that bucket: |
| 82 | +
|
| 83 | +```codebyte/cpp |
| 84 | +#include <iostream> |
| 85 | +#include <unordered_set> |
| 86 | +using namespace std; |
| 87 | +
|
| 88 | +int main() { |
| 89 | + unordered_set<string> words = {"cat", "dog", "rabbit", "lion"}; |
| 90 | +
|
| 91 | + size_t bucket = 0; |
| 92 | +
|
| 93 | + auto it = words.cbegin(bucket); |
| 94 | + auto end = words.cend(bucket); |
| 95 | +
|
| 96 | + cout << "Elements in bucket " << bucket << ":\n"; |
| 97 | +
|
| 98 | + for (; it != end; ++it) { |
| 99 | + cout << " " << *it << "\n"; |
| 100 | + } |
| 101 | +
|
| 102 | + return 0; |
| 103 | +} |
| 104 | +``` |
0 commit comments