Skip to content

Commit 6d9ab86

Browse files
content fixes
1 parent 751a431 commit 6d9ab86

File tree

1 file changed

+49
-46
lines changed
  • content/cpp/concepts/unordered-set/terms/cbegin

1 file changed

+49
-46
lines changed
Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,102 @@
11
---
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.'
44
Subjects:
5+
- 'Code Foundations'
56
- 'Computer Science'
6-
- 'Programming'
77
Tags:
8-
- 'C++'
9-
- 'Unordered Set'
10-
- 'STL'
118
- 'Iterators'
9+
- 'Sets'
10+
- 'STL'
1211
CatalogContent:
1312
- 'learn-c-plus-plus'
1413
- 'paths/computer-science'
1514
---
1615

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.
2017

2118
## Syntax
2219

23-
The `.cbegin()` method is called directly on the `unordered_set` object.
20+
```pseudo
21+
unordered_set_name.cbegin(n);
22+
```
2423

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);
2732
```
2833

29-
## Parameters
34+
**Parameters:**
3035

31-
The method takes no parameters.
36+
- `n` (size_type): The bucket index. Must be less than `bucket_count()`.
3237

33-
## Return Value
38+
**Return value:**
3439

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)`.
3641

3742
## Example
3843

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()`:
4045

4146
```cpp
4247
#include <iostream>
4348
#include <string>
4449
#include <unordered_set>
45-
#include <algorithm> // Required for std::find or general use
4650

4751
int main() {
4852
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();
5253

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();
5555

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";
5957

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+
}
6162

62-
// The following line would cause a compile-time error:
63-
// *it = 99;
63+
// *it = 99; // Error: cannot modify through const_iterator
6464

6565
return 0;
6666
}
6767
```
6868

69-
Output:
69+
A sample output of this code is:
7070

71-
```
71+
```shell
7272
The first element in the set's internal order is: 20
7373
The second element is: 5
7474
```
7575
76-
## Codebyte
76+
> **Note:** Attempting to modify the element pointed to by the `const_iterator` would result in a compilation error.
7777
78-
Use the Codebyte below to access the first element of an `unordered_set` using the constant iterator returned by `.cbegin()`.
78+
## Codebyte Example
7979
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
8183
#include <iostream>
8284
#include <unordered_set>
8385
8486
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+
}
9699
97100
return 0;
98101
}
99-
```
102+
```

0 commit comments

Comments
 (0)