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


C++ unordered_multiset bucket_count()用法及代码示例


unordered_multiset::bucket_count()是C++ STL中的内置函数,该函数返回unordered_multiset容器中的存储桶总数。值区是容器内部哈希表中的一个插槽,根据其哈希值将元素分配给该插槽。

用法:

unordered_multiset_name.bucket_count()

参数:该函数不接受任何参数。


返回值:它返回一个无符号整数类型,表示桶的总数。

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

示例1:

// C++ program to illustrate the 
// unordered_multiset::bucket_count() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<char> sample; 
  
    // inserts element 
    sample.insert('a'); 
    sample.insert('b'); 
    sample.insert('b'); 
    sample.insert('b'); 
    sample.insert('z'); 
  
    cout << "The total count of buckets: " << sample.bucket_count(); 
  
    // prints all element bucket wise 
    for (int i = 0; i < sample.bucket_count(); i++) { 
  
        cout << "\nBucket " << i << ": "; 
  
        // if bucket is empty 
        if (sample.bucket_size(i) == 0) 
            cout << "empty"; 
  
        for (auto it = sample.cbegin(i); it != sample.cend(i); it++) 
            cout << *it << " "; 
    } 
    return 0; 
}
输出:
The total count of buckets: 7
Bucket 0: b b b 
Bucket 1: empty
Bucket 2: empty
Bucket 3: z 
Bucket 4: empty
Bucket 5: empty
Bucket 6: a

示例2:

// C++ program to illustrate the 
// unordered_multiset::bucket_count() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<char> sample; 
  
    // inserts element 
    sample.insert('a'); 
    sample.insert('b'); 
    sample.insert('b'); 
    sample.insert('b'); 
    sample.insert('z'); 
  
    cout << "The total count of buckets: " << sample.bucket_count(); 
  
    // prints all element bucket wise 
    for (int i = 0; i < sample.bucket_count(); i++) { 
  
        cout << "\nBucket " << i << ": "; 
  
        // if bucket is empty 
        if (sample.bucket_size(i) == 0) 
            cout << "empty"; 
  
        for (auto it = sample.cbegin(i); it != sample.cend(i); it++) 
            cout << *it << " "; 
    } 
    return 0; 
}
输出:
The total count of buckets: 7
Bucket 0: b b b 
Bucket 1: empty
Bucket 2: empty
Bucket 3: z 
Bucket 4: empty
Bucket 5: empty
Bucket 6: a


相关用法


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