×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

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.

C++ STL 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.

Syntax

size_type bucket ( const key_type& k ) const;

// or
unordered_map.bucket(key);

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.

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;
}

Output

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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.