📌  相关文章
📜  C++中unordered_map中的bucket_count和bucket_size

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

众所周知,内部unordered_map是使用哈希表实现的,因此存储桶是内部哈希表中的一个插槽,根据其键的哈希值将元素分配给该插槽。存储桶的编号从0到(bucket_count-1)。因此,此函数返回存储桶编号。其中具有键的元素位于unordered_map中。
时间复杂度:O(1)。

句法:

unordered_map.bucket(k);
k is the key corresponds to which we want to know bucket number.
Returns: The order number of the bucket corresponding to key k.

关于存储桶,还有另外两个功能:
1. std :: bucket_count:此函数用于计算总数。 unordered_map中的存储桶数。不需要任何参数即可传递给此函数。
时间复杂度:O(1)。

句法:

unordered_map.bucket_count();
Returns: The number of the bucket present in hash table of unordered_map.

2. std :: bucket_size:此函数计算unordered_map的每个存储桶中存在的元素数。
时间复杂度:时段大小呈线性关系。

句法:

unordered_map.bucket_size(i);
where 'i' is the bucket number in which we want 
to find no. of elements. (i < bucket_count)
Returns: The number of elements present in bucket 'i'.
// C++ program to demonstrate the use of std::bucket
#include 
using namespace std;
  
int main()
{
    // Declaring umap to be of  type
    // key will be of string type and mapped value will
    // be of double type
    unordered_map umap;
  
    // inserting values by using [] operator
    umap["PI"] = 3.14;
    umap["root2"] = 1.414;
    umap["log10"] = 2.302;
    umap["loge"] = 1.0;
    umap["e"] = 2.718;
  
    // Display bucket no. where key, value pair is located
    // using bucket(key)
    for (auto& x : umap) {
        cout << "(" << x.first << ", " << x.second << ")";
        cout << " is in bucket= " 
            << umap.bucket(x.first) << endl;
    }
    cout << endl;
  
    // Count the no.of buckets in the unordered_map 
    // using bucket_count()
    int n = umap.bucket_count();
    cout << "umap has " << n << " buckets.\n\n";
  
    // Count no. of elements in each bucket using 
    // bucket_size(position)
    for (int i = 0; i < n; i++) {
        cout << "Bucket " << i << " has " 
             << umap.bucket_size(i) << " elements.\n";
    }
  
    return 0;
}

输出:

(PI, 3.14) is in bucket= 5
(e, 2.718) is in bucket= 1
(root2, 1.414) is in bucket= 1
(log10, 2.302) is in bucket= 10
(loge, 1) is in bucket= 7

umap has 11 buckets.

Bucket 0 has 0 elements.
Bucket 1 has 2 elements.
Bucket 2 has 0 elements.
Bucket 3 has 0 elements.
Bucket 4 has 0 elements.
Bucket 5 has 1 elements.
Bucket 6 has 0 elements.
Bucket 7 has 1 elements.
Bucket 8 has 0 elements.
Bucket 9 has 0 elements.
Bucket 10 has 1 elements.

我们还可以打印unordered_map每个存储桶中存在的所有元素。

// C++ program to print all elements present in each bucket
#include 
using namespace std;
  
int main()
{
    // Declaring umap to be of  type
    // key will be of string type and mapped value 
    // will be of double type
    unordered_map umap;
  
    // inserting values by using [] operator
    umap["PI"] = 3.14;
    umap["root2"] = 1.414;
    umap["log10"] = 2.302;
    umap["loge"] = 1.0;
    umap["e"] = 2.718;
  
    unsigned n = umap.bucket_count();
  
    // Prints elements present in each bucket
    for (unsigned i = 0; i < n; i++) {
        cout << "Bucket " << i << " contains: ";
        for (auto it = umap.begin(i); it != umap.end(i); it++)
            cout << "(" << it->first << ", " 
                 << it->second << ") ";
        cout << "\n";
    }
    return 0;
}

输出:

Bucket 0 contains: 
Bucket 1 contains: (e, 2.718) (root2, 1.414) 
Bucket 2 contains: 
Bucket 3 contains: 
Bucket 4 contains: 
Bucket 5 contains: (PI, 3.14) 
Bucket 6 contains: 
Bucket 7 contains: (loge, 1) 
Bucket 8 contains: 
Bucket 9 contains: 
Bucket 10 contains: (log10, 2.302) 

std :: unordered_map中存储桶的使用:有许多算法要求将对象散列到一定数量的存储桶中,然后处理每个存储桶。假设您要在集合中查找重复项。您对集合中的所有项目进行哈希处理,然后在每个存储桶中成对比较项目。一个简单的例子是用于查找频繁项集的Apriori算法。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”