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:
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_multimap< int , int > sample;
sample.insert({ 10, 100 });
sample.insert({ 10, 100 });
sample.insert({ 20, 200 });
sample.insert({ 30, 300 });
sample.insert({ 15, 150 });
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;
}
|
Output:
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:
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_multimap< char , char > sample;
sample.insert({ 'a' , 'b' });
sample.insert({ 'a' , 'b' });
sample.insert({ 'b' , 'c' });
sample.insert({ 'r' , 'a' });
sample.insert({ 'c' , 'b' });
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;
}
|
Output:
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
08 Aug, 2018
Like Article
Save Article