当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ unordered_multimap bucket()用法及代码示例


unordered_multimap::bucket()是C++ STL中的内置函数,该函数返回给定键所在的存储区编号。铲斗尺寸从0到bucket_count-1不等。

用法:

unordered_multimap_name.bucket(key)

参数:该函数接受单个强制性参数 key ,该 key 指定要返回其存储区编号的 key 。


返回值:它返回一个无符号整数类型,该整数类型表示 key 所在的存储区编号。

以下示例程序旨在说明上述函数:

示例1:

// C++ program to illustrate the 
// unordered_multimap::bucket()  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<int, int> sample; 
  
    // inserts key and element 
    sample.insert({ 10, 100 }); 
    sample.insert({ 10, 100 }); 
    sample.insert({ 20, 200 }); 
    sample.insert({ 30, 300 }); 
    sample.insert({ 15, 150 }); 
  
    // iterate for all elements and print its bucket number 
    for (auto it = sample.begin();  
                  it != sample.end(); it++) { 
        cout << "The bucket number in which {" 
             << it->first << ", " 
             << it->second << "} is " 
             << sample.bucket(it->first) << endl; 
    } 
    return 0; 
}
输出:
The bucket number in which {15, 150} is 1
The bucket number in which {30, 300} is 2
The bucket number in which {20, 200} is 6
The bucket number in which {10, 100} is 3
The bucket number in which {10, 100} is 3

示例2:

// C++ program to illustrate the 
// unordered_multimap::bucket()  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<char, char> sample; 
  
    // inserts key and element 
    sample.insert({ 'a', 'b' }); 
    sample.insert({ 'a', 'b' }); 
    sample.insert({ 'b', 'c' }); 
    sample.insert({ 'r', 'a' }); 
    sample.insert({ 'c', 'b' }); 
  
    // iterate for all elements and print its bucket number 
    for (auto it = sample.begin();  
                    it != sample.end(); it++) { 
        cout << "The bucket number in which {" 
             << it->first << ", "
             << it->second << "} is " 
             << sample.bucket(it->first) << endl; 
    } 
    return 0; 
}
输出:
The bucket number in which {c, b} is 1
The bucket number in which {r, a} is 2
The bucket number in which {b, c} is 0
The bucket number in which {a, b} is 6
The bucket number in which {a, b} is 6


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 unordered_multimap bucket() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。