unordered_map bucket() Function in C++ with Examples

C++ unordered_map bucket() Function: Here, we will learn about the bucket() function, its usages, syntax and examples.
Submitted by Shivang Yadav, on May 26, 2022


An unordered_map is a special type of container that stores data in the form of key-value pairs in an unordered manner. The key stored is used to identify the data value mapped to it uniquely.

std::unordered_map::bucket() Function

The bucket() function in the unordered_map structure is used to return the bucket number that contains the element with key k.


size_type bucket ( const key_type& k ) const;

// or

Parameter(s): It accepts a single parameter which is the key whose bucket number is to be found.

Return Value: It returns a single integer value which is the bucket number containing the key.

C++ unordered_map bucket() Function Example:

#include <bits/stdc++.h>
using namespace std;

int main()
    unordered_map<char, string> myProgLang;
    myProgLang = { 
        { 'S', "Scala" }, 
        { 'P', "Python" }, 
        { 'J', "JavaScript" }, 
        { 'C', "C++" } 

    auto i = myProgLang.begin();
    int bucketNum = myProgLang.bucket(i->first);
    cout << "For the key " << i->first << ", value is : " << i->second;
    cout << "\nBucket number is " << bucketNum;
    return 0;


For the key J, value is : JavaScript
Bucket number is 9

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.