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


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


unordered_multiset::bucket_size()是C++ STL中的內置函數,該函數返回存儲區中具有元素val的元素數。它總是低於bucket_count。存儲桶中元素的數量會影響訪問存儲桶中特定元素所花費的時間

用法:

unordered_multiset_name.bucket_size(val)

參數:該函數接受參數val,該參數指定要返回其存儲桶大小的元素。


返回值:它返回一個無符號整數類型,該整數類型表示存儲區中具有元素val的元素數。

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

示例1:

// C++ program to illustrate the 
// unordered_multiset::bucket_size() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<int> sample; 
  
    // inserts element 
    sample.insert(11); 
    sample.insert(11); 
    sample.insert(11); 
    sample.insert(12); 
    sample.insert(13); 
    sample.insert(13); 
    sample.insert(14); 
  
    for (auto it = sample.begin(); it != sample.end(); it++) { 
        cout << "The bucket size in which " << *it 
             << " is " << sample.bucket_size(*it) << endl; 
    } 
    return 0; 
}
輸出:
The bucket size in which 14 is 1
The bucket size in which 11 is 3
The bucket size in which 11 is 3
The bucket size in which 11 is 3
The bucket size in which 12 is 1
The bucket size in which 13 is 2
The bucket size in which 13 is 2

示例2:

// C++ program to illustrate the 
// unordered_multiset::bucket_size() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<int> sample; 
  
    // inserts element 
    sample.insert(1); 
    sample.insert(1); 
    sample.insert(1); 
    sample.insert(2); 
    sample.insert(3); 
    sample.insert(4); 
    sample.insert(3); 
  
    for (auto it = sample.begin(); it != sample.end(); it++) { 
        cout << "The bucket size in which " << *it 
             << " is " << sample.bucket_size(*it) << endl; 
    } 
    return 0; 
}
輸出:
The bucket size in which 1 is 3
The bucket size in which 1 is 3
The bucket size in which 1 is 3
The bucket size in which 2 is 1
The bucket size in which 3 is 2
The bucket size in which 3 is 2
The bucket size in which 4 is 1


相關用法


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