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

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

unordered_set :: bucket_count()方法是C++ STL中的内置函数,它返回unordered_set容器中存在的存储桶总数。

铲斗处于unordered_set的内部散列表的狭槽,其中元素被存储。

注意:unordered_set中的存储桶编号从0到n-1,其中n是存储桶的总数。

语法

unordered_set_name.bucket_count();

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

返回值:该函数返回unordered_set容器中存在的存储桶的当前计数。

下面的程序说明了unordered_set :: bucket_count()函数:

// CPP program to illustrate the
// unordered_set::bucket_count() function
  
#include 
#include 
  
using namespace std;
  
int main()
{
  
    unordered_set sampleSet;
  
    // Inserting elements
    sampleSet.insert(5);
    sampleSet.insert(10);
    sampleSet.insert(15);
    sampleSet.insert(20);
    sampleSet.insert(25);
  
    cout << "The sampleSet container has " << sampleSet.bucket_count()
         << " number of buckets\n\n";
  
    for (auto itr = sampleSet.begin(); itr != sampleSet.end(); itr++) {
        cout << "The Element " << (*itr) << " is present in the bucket: "
             << sampleSet.bucket(*itr);
        cout << endl;
    }
  
    return 0;
}
输出:
The sampleSet container has 11 number of buckets

The Element 25 is present in the bucket: 3
The Element 5 is present in the bucket: 5
The Element 10 is present in the bucket: 10
The Element 15 is present in the bucket: 4
The Element 20 is present in the bucket: 9
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”