unordered_map bucket() in C++ STL

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the
// unordered_map::bucket() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Declaration
    unordered_map<string, string> mymap;
  
    // Initilisation
    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;
}

chevron_right


Output:

The bucket number of key France is 3


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.