C++ STL中unordered_set bucket()函数
在C++ STL中, unordered_set::bucket() 方法是一种内置函数,它返回特定元素的桶编号。也就是说,这个函数返回unordered_set容器中存储特定元素的桶编号。 bucket 是unordered_set内部哈希表中存储元素的插槽。 注意: unordered_set中的桶编号从0到n-1,其中n是桶的总数。
语法:
unordered_set_name_.bucket(element);
参数: 这是一个必需参数,用于指定需要了解unordered_set容器中哪个元素的桶编号。
返回值: 此函数返回包含值为 element的元素的桶在unordered_set容器中的桶编号。下面的程序说明了 unordered_set::bucket()函数:
//C++程序演示unordered_set :: bucket()函数
 
#include <iostream>
#include <unordered_set>
 
using namespace std;
 
int main()
{
 
    unordered_set<int> sampleSet;
 
    // 插入元素
    sampleSet.insert(5);
    sampleSet.insert(10);
    sampleSet.insert(15);
    sampleSet.insert(20);
    sampleSet.insert(25);
 
    for (auto itr = sampleSet.begin(); itr != sampleSet.end(); itr++) {
        cout << "元素 " << (*itr) << " 存储在桶中:"
            << sampleSet.bucket(*itr);
        cout << endl;
    }
 
    返回0;
}  
输出:
元素25存储在桶中:3
元素5存储在桶中:5
元素10存储在桶中:10
元素15存储在桶中:4
元素20存储在桶中:9
时间复杂度: O(1)
极客教程