Skip to content

Commit 2c3d184

Browse files
-Add example and codebyte
1 parent 23fb8a9 commit 2c3d184

File tree

1 file changed

+33
-23
lines changed
  • content/cpp/concepts/unordered-set/terms/bucket

1 file changed

+33
-23
lines changed
Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Title: 'Bucket()'
3-
Description: 'Uses a hash function internally to organize elements into various "buckets" to facilitate fast lookups. Allows a programmer to inspect the internal distribution of elements.'
3+
Description: Uses a hash function internally to organize elements into various buckets to facilitate fast lookups. Allows a programmer to inspect the internal distribution of elements.
44
Subjects:
55
- 'Code Foundations'
66
- 'Computer Science'
@@ -15,51 +15,61 @@ CatalogContent:
1515
---
1616
# bucket()
1717

18-
In C++, the `bucket()` function returns the bucket number where a specific element is stored in the `unordered_set` container.
19-
20-
The *bucket* is a slot in the `unordered_set`'s internal hash table where elements are stored.
21-
18+
In C++, the `bucket()` function returns the index of the bucket in which a specific element is stored within an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set).
2219

20+
It can be used to understand the internal hash table structure, check the distribution of elements, or implement custom traversal logic.
2321

2422
## Syntax
25-
23+
### Syntax to locate specific element
2624
```psuedo
27-
size_ type bucket( const key& key) const;
25+
unordered_set.bucket(x);
2826
```
2927

30-
## Example
28+
**Parameters:**
29+
- `x`: The element value you are locating.
30+
31+
**Return Value:**
32+
33+
The `bucket()` function returns the index of the bucket containing the specified element. If the element is not present, it returns the index for the bucket where the element would be placed based on its hash value.
34+
35+
## Example: Using .bucket() to locate a specific element
3136

3237
```cpp
3338
#include <iostream>
3439
#include <string>
3540
#include <unordered_set>
3641

37-
int main ()
38-
{
39-
std::unordered_set<std::string> myset = {"earth","wind","water","fire"};
42+
int main() {
43+
std::unordered_set<std::string> benders = {"earth","air","water","fire"};
4044

41-
for (const std::string& x: myset) {
42-
std::cout << x << " is in bucket #" << myset.bucket(x) << std::endl;
43-
}
45+
//Find bucket index for a specific element
46+
std::cout << "airbenders are in bucket: " << benders.bucket("air") << std::endl;
4447

45-
return 0;
48+
return 0;
4649
}
4750
```
4851
The output for this code is:
4952
```cpp
50-
water is in bucket #0
51-
fire is in bucket #2
52-
wind is in bucket #2
53-
earth is in bucket #4
53+
airbenders are in bucket: 0
5454
```
5555

56-
## Codebyte Example (if applicable)
56+
> **Note:** The output may vary depending on the specific C++ implementation and hash function.
5757
5858

59+
## Codebyte Example: Iterating Through a Set
5960

60-
See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details!
6161

6262
```codebyte/cpp
63-
# Example runnable code block.
64-
console.log('Hello, World!');
63+
#include <iostream>
64+
#include <string>
65+
#include <unordered_set>
66+
67+
int main() {
68+
std::unordered_set<std::string> houses = {"gryffindor", "hufflepuff", "slytherin", "ravenclaw"};
69+
70+
for (const std::string& x: houses) {
71+
std::cout << x << " house is in bucket: " << houses.bucket(x) << std::endl;
72+
}
73+
return 0;
74+
}
6575
```

0 commit comments

Comments
 (0)