📌  相关文章
📜  C++ STL中的unordered_multiset bucket_count()函数

📅  最后修改于: 2021-05-30 11:17:30             🧑  作者: Mango

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 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset 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 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset 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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”