unordered_multimap bucket() function in C++ STL
The unordered_multimap::bucket() is a built-in function in C++ STL which returns the bucket number in which a given key is. Bucket size varies from 0 to bucket_count-1.
Syntax:
unordered_multimap_name.bucket(key)
Parameters: The function accepts a single mandatory parameter key which specifies the key whose bucket number is to be returned.
Return Value: It returns an unsigned integral type which signifies the bucket number in which the key is.
Below programs illustrates the above function:
Program 1:
// C++ program to illustrate the // unordered_multimap::bucket() #include <bits/stdc++.h> using namespace std; int main() { // declaration unordered_multimap<int, int> sample; // inserts key and element sample.insert({ 10, 100 }); sample.insert({ 10, 100 }); sample.insert({ 20, 200 }); sample.insert({ 30, 300 }); sample.insert({ 15, 150 }); // iterate for all elements and print its bucket number for (auto it = sample.begin(); it != sample.end(); it++) { cout << "The bucket number in which {" << it->first << ", " << it->second << "} is " << sample.bucket(it->first) << endl; } return 0; } |
The bucket number in which {15, 150} is 1
The bucket number in which {30, 300} is 2
The bucket number in which {20, 200} is 6
The bucket number in which {10, 100} is 3
The bucket number in which {10, 100} is 3
Program 2:
// C++ program to illustrate the // unordered_multimap::bucket() #include <bits/stdc++.h> using namespace std; int main() { // declaration unordered_multimap<char, char> sample; // inserts key and element sample.insert({ 'a', 'b' }); sample.insert({ 'a', 'b' }); sample.insert({ 'b', 'c' }); sample.insert({ 'r', 'a' }); sample.insert({ 'c', 'b' }); // iterate for all elements and print its bucket number for (auto it = sample.begin(); it != sample.end(); it++) { cout << "The bucket number in which {" << it->first << ", " << it->second << "} is " << sample.bucket(it->first) << endl; } return 0; } |
The bucket number in which {c, b} is 1
The bucket number in which {r, a} is 2
The bucket number in which {b, c} is 0
The bucket number in which {a, b} is 6
The bucket number in which {a, b} is 6
Recommended Posts:
- unordered_multiset bucket() function in C++ STL
- unordered_set bucket() function in C++ STL
- unordered_map bucket() in C++ STL
- Function Overloading vs Function Overriding in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- log() function in C++
- exp() function C++
- div() function in C++
- fma() function in C++
- strtoimax() function in C++
- wctype() function in C/C++
- iswctype() function in C/C++
- strtoumax() function in C++
- mbsinit() Function in C/C++
- bit_and function in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



