C++ Unordered_set 库-bucket_size


描述

它返回桶 n 中的元素数量。

宣言

以下是 std::unordered_set::bucket_size 的声明。

C++11

size_type bucket_size ( size_type n ) const;

参数

n - 它包含有关桶号的信息。

返回值

它返回桶 n 中的元素数量。

例外情况

如果任何元素比较对象抛出异常,则抛出异常。

请注意,无效参数会导致未定义的行为。

时间复杂度

恒定时间。

例子

以下示例显示了 std::unordered_set::bucket_size 的用法。

#include <iostream>
#include <string>
#include <unordered_set>

int main () {
   std::unordered_set<std::string> myset =
      { "sai", "ram", "krishna", "prasad", "tutorials", "point" };

   unsigned nbuckets = myset.bucket_count();

   std::cout << "myset has " << nbuckets << " buckets:\n";

   for (unsigned i = 0; i < nbuckets; ++i) {
      std::cout << "bucket #" << i << " has " << myset.bucket_size(i) << " elements.\n";
   }

   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

myset has 7 buckets:
bucket #0 has 1 elements.
bucket #1 has 1 elements.
bucket #2 has 0 elements.
bucket #3 has 0 elements.
bucket #4 has 2 elements.
bucket #5 has 1 elements.
bucket #6 has 1 elements.
无序集.htm