Skip to content
Related Articles

Related Articles

Improve Article

unordered_map bucket() in C++ STL

  • Difficulty Level : Medium
  • Last Updated : 11 Jun, 2021

The unordered_map::bucket() is a built-in STL function in C++ which returns the bucket number where the element with the key k is located in the map. 
Syntax: 
 

size_type bucket(key) 

Parameter: The function accepts one mandatory parameter key which specifies the key whose bucket number is to be returned. 
Return Value: This method returns an unsigned integral type which represents the bucket number of the key k which is passed in the parameter. 
Below program illustrate the unordered_map::bucket() function: 
 

CPP




// CPP program to demonstrate the
// unordered_map::bucket() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Declaration
    unordered_map<string, string> mymap;
 
    // Initialisation
    mymap = { { "Australia", "Canberra" },
              { "U.S.", "Washington" },
              { "France", "Paris" } };
 
    // prints the bucket number of the beginning element
    auto it = mymap.begin();
 
    // stores the bucket number of the key k
    int number = mymap.bucket(it->first);
    cout << "The bucket number of key " << it->first
                                     << " is " << number;
 
    return 0;
}
Output: 
The bucket number of key France is 3

 

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 :