|
1 | 1 | --- |
2 | | -Title: '.cbegin()' |
3 | | -Description: 'Returns a constant iterator pointing to the first element of the unordered_set.' |
| 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 | 4 | Subjects: |
| 5 | + - 'Code Foundations' |
5 | 6 | - 'Computer Science' |
6 | | - - 'Programming' |
7 | 7 | Tags: |
8 | | - - 'C++' |
9 | | - - 'Unordered Set' |
10 | | - - 'STL' |
11 | 8 | - 'Iterators' |
| 9 | + - 'Sets' |
| 10 | + - 'STL' |
12 | 11 | CatalogContent: |
13 | 12 | - 'learn-c-plus-plus' |
14 | 13 | - 'paths/computer-science' |
15 | 14 | --- |
16 | 15 |
|
17 | | -The **`.cbegin()`** method returns a **constant iterator** that points to the first element in an `std::unordered_set`. |
18 | | - |
19 | | -The key feature of a constant iterator (`const_iterator`) is that it allows you to **read** the elements but **prevents** you from modifying them. This is a best practice when iterating over a set to ensure data integrity. Since `unordered_set` does not guarantee any specific order, the element returned by `cbegin()` depends on the internal hash table structure. |
| 16 | +The **`cbegin()`** method returns a constant iterator that points to the first element of an `std::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. |
20 | 17 |
|
21 | 18 | ## Syntax |
22 | 19 |
|
23 | | -The `.cbegin()` method is called directly on the `unordered_set` object. |
| 20 | +```pseudo |
| 21 | +unordered_set_name.cbegin(n); |
| 22 | +``` |
24 | 23 |
|
25 | | -```cpp |
26 | | -unordered_set_name.cbegin(); |
| 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); |
27 | 32 | ``` |
28 | 33 |
|
29 | | -## Parameters |
| 34 | +**Parameters:** |
30 | 35 |
|
31 | | -The method takes no parameters. |
| 36 | +- `n` (size_type): The bucket index. Must be less than `bucket_count()`. |
32 | 37 |
|
33 | | -## Return Value |
| 38 | +**Return value:** |
34 | 39 |
|
35 | | -Returns a `const_iterator` (constant iterator) pointing to the first element in the `unordered_set`. |
| 40 | +A `const_local_iterator` pointing to the first element in bucket `n`. If the bucket is empty, the returned iterator equals `cend(n)`. |
36 | 41 |
|
37 | 42 | ## Example |
38 | 43 |
|
39 | | -This example demonstrates obtaining the starting element of an `unordered_set` using `cbegin()`. Note that attempting to modify the element pointed to by the `const_iterator` would result in a compilation error. |
| 44 | +This example demonstrates obtaining the starting element of an `unordered_set` using `cbegin()`: |
40 | 45 |
|
41 | 46 | ```cpp |
42 | 47 | #include <iostream> |
43 | 48 | #include <string> |
44 | 49 | #include <unordered_set> |
45 | | -#include <algorithm> // Required for std::find or general use |
46 | 50 |
|
47 | 51 | int main() { |
48 | 52 | std::unordered_set<int> unique_numbers = {10, 5, 20, 15}; |
49 | | - |
50 | | - // Get a constant iterator to the beginning |
51 | | - auto it = unique_numbers.cbegin(); |
52 | 53 |
|
53 | | - // Print the value of the first element (read-only access) |
54 | | - std::cout << "The first element in the set's internal order is: " << *it << "\n"; |
| 54 | + auto it = unique_numbers.cbegin(); |
55 | 55 |
|
56 | | - // Advance the iterator to the next element |
57 | | - // Note: The order of elements is not guaranteed! |
58 | | - ++it; |
| 56 | + std::cout << "The first element in internal order is: " << *it << "\n"; |
59 | 57 |
|
60 | | - std::cout << "The second element is: " << *it << "\n"; |
| 58 | + ++it; |
| 59 | + if (it != unique_numbers.cend()) { |
| 60 | + std::cout << "The second element is: " << *it << "\n"; |
| 61 | + } |
61 | 62 |
|
62 | | - // The following line would cause a compile-time error: |
63 | | - // *it = 99; |
| 63 | + // *it = 99; // Error: cannot modify through const_iterator |
64 | 64 |
|
65 | 65 | return 0; |
66 | 66 | } |
67 | 67 | ``` |
68 | 68 |
|
69 | | -Output: |
| 69 | +A sample output of this code is: |
70 | 70 |
|
71 | | -``` |
| 71 | +```shell |
72 | 72 | The first element in the set's internal order is: 20 |
73 | 73 | The second element is: 5 |
74 | 74 | ``` |
75 | 75 |
|
76 | | -## Codebyte |
| 76 | +> **Note:** Attempting to modify the element pointed to by the `const_iterator` would result in a compilation error. |
77 | 77 |
|
78 | | -Use the Codebyte below to access the first element of an `unordered_set` using the constant iterator returned by `.cbegin()`. |
| 78 | +## Codebyte Example |
79 | 79 |
|
80 | | -```cpp |
| 80 | +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: |
| 81 | +
|
| 82 | +```codebyte/cpp |
81 | 83 | #include <iostream> |
82 | 84 | #include <unordered_set> |
83 | 85 |
|
84 | 86 | int main() { |
85 | | - std::unordered_set<std::string> colors = {"red", "green", "blue", "yellow"}; |
86 | | - |
87 | | - // Use cbegin() to get the constant iterator |
88 | | - auto start_it = colors.cbegin(); |
89 | | - |
90 | | - // Print the value the iterator points to |
91 | | - std::cout << "First element: " << *start_it << "\n"; |
92 | | - |
93 | | - // Advance the iterator |
94 | | - ++start_it; |
95 | | - std::cout << "Second element: " << *start_it << "\n"; |
| 87 | + std::unordered_set<std::string> words = {"cat", "dog", "rabbit", "lion"}; |
| 88 | +
|
| 89 | + size_t bucket = 0; |
| 90 | +
|
| 91 | + auto it = words.cbegin(bucket); |
| 92 | + auto end = words.cend(bucket); |
| 93 | +
|
| 94 | + std::cout << "Elements in bucket " << bucket << ":\n"; |
| 95 | +
|
| 96 | + for (; it != end; ++it) { |
| 97 | + std::cout << " " << *it << "\n"; |
| 98 | + } |
96 | 99 |
|
97 | 100 | return 0; |
98 | 101 | } |
99 | | -``` |
| 102 | +``` |
0 commit comments