Skip to content
Related Articles

Related Articles

Improve Article

unordered_multiset bucket() function in C++ STL

  • Last Updated : 02 Aug, 2018

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:

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:
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

Program 2:




// 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
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :