當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。