The unordered_multiset::bucket() is a built-in function in C++ STL which returns the bucket number in which a given element is. Bucket size varies from 0 to bucket_count-1.
Syntax:
CPP
CPP
unordered_multiset_name.bucket(element)Parameters: The function accepts a single mandatory element which specifies the value whose bucket number is to be returned. Return Value: It returns an unsigned integral type which signifies the bucket number in which the element is. Below programs illustrates the above function: Program 1:
// C++ program to illustrate the
// unordered_multiset::bucket() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
unordered_multiset<int> sample;
// inserts element
sample.insert(10);
sample.insert(15);
sample.insert(15);
sample.insert(13);
sample.insert(13);
for (auto it = sample.begin(); it != sample.end(); it++) {
cout << "The bucket number in which " << *it
<< " is " << sample.bucket(*it) << endl;
}
return 0;
}
Output:
Program 2:
The bucket number in which 13 is 6 The bucket number in which 13 is 6 The bucket number in which 10 is 3 The bucket number in which 15 is 1 The bucket number in which 15 is 1
// C++ program to illustrate the
// unordered_multiset::bucket() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
unordered_multiset<char> sample;
// inserts element
sample.insert('a');
sample.insert('a');
sample.insert('b');
sample.insert('b');
sample.insert('c');
sample.insert('c');
sample.insert('e');
for (auto it = sample.begin(); it != sample.end(); it++) {
cout << "The bucket number in which " << *it
<< " is " << sample.bucket(*it) << endl;
}
return 0;
}
Output:
The bucket number in which e is 16 The bucket number in which a is 12 The bucket number in which a is 12 The bucket number in which b is 13 The bucket number in which b is 13 The bucket number in which c is 14 The bucket number in which c is 14