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


C++ unordered_multimap bucket_size()用法及代碼示例


unordered_multimap::bucket_size()是C++ STL中的內置函數,該函數返回存儲區n中的元素數。

用法:

unordered_multimap_name.bucket_size(n)

參數:該函數接受參數n,該參數指定要返回其計數的存儲區編號。


返回值:它返回一個無符號整數類型,該整數類型表示存儲桶中編號為n的存儲單元的數量。

以下示例程序旨在說明上述函數:

示例1:

// C++ program to illustrate the 
// unordered_multimap::bucket_size() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<int, int> sample; 
  
    // inserts element 
    sample.insert({ 10, 100 }); 
    sample.insert({ 10, 100 }); 
    sample.insert({ 20, 200 }); 
    sample.insert({ 30, 300 }); 
    sample.insert({ 15, 150 }); 
  
    cout << "The total count of buckets: " 
         << sample.bucket_count(); 
  
    // prints all element bucket wise 
    for (int i = 0; i < sample.bucket_count(); i++) { 
  
        cout << "\nNumber of elements in Bucket " << i 
             << " = " << sample.bucket_size(i); 
    } 
    return 0; 
}
輸出:
The total count of buckets: 7
Number of elements in Bucket 0 = 0
Number of elements in Bucket 1 = 1
Number of elements in Bucket 2 = 1
Number of elements in Bucket 3 = 2
Number of elements in Bucket 4 = 0
Number of elements in Bucket 5 = 0
Number of elements in Bucket 6 = 1

示例2:

// C++ program to illustrate the 
// unordered_multimap::bucket_size()  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<char, char> sample; 
  
    // inserts element 
    sample.insert({ 'a', 'b' }); 
    sample.insert({ 'a', 'b' }); 
    sample.insert({ 'b', 'c' }); 
    sample.insert({ 'r', 'a' }); 
    sample.insert({ 'c', 'b' }); 
  
    cout << "The total count of buckets: " 
         << sample.bucket_count(); 
  
    // prints all element bucket wise 
    for (int i = 0; i < sample.bucket_count(); i++) { 
  
        cout << "\nNumber of elements in Bucket " << i 
             << " = " << sample.bucket_size(i); 
    } 
    return 0; 
}
輸出:
The total count of buckets: 7
Number of elements in Bucket 0 = 1
Number of elements in Bucket 1 = 1
Number of elements in Bucket 2 = 1
Number of elements in Bucket 3 = 0
Number of elements in Bucket 4 = 0
Number of elements in Bucket 5 = 0
Number of elements in Bucket 6 = 2


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 unordered_multimap bucket_size() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。