ADVERTISEMENT
ADVERTISEMENT

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

unordered_map:

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.

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.

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

Output:

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

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.