C++ Unordered_set 库-bucket_count


描述

它返回 unordered_set 容器中的桶数。

宣言

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

C++11

size_type bucket_count() const noexcept;

参数

没有任何

返回值

它返回 unordered_set 容器中的桶数。

例外情况

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

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

时间复杂度

恒定时间。

例子

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

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

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

   unsigned n = myset.bucket_count();

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

   for (unsigned i = 0; i < n; ++i) {
      std::cout << "bucket #" << i << " contains:";
      for (auto it = myset.begin(i); it!=myset.end(i); ++it)
      std::cout << " " << *it;
      std::cout << "\n";
   }

   return 0;
}

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

myset has 11 buckets.
bucket #0 contains:
bucket #1 contains:
bucket #2 contains: tutorials sai
bucket #3 contains: krishna ram
bucket #4 contains:
bucket #5 contains:
bucket #6 contains: mammahe prasad
bucket #7 contains:
bucket #8 contains: com
bucket #9 contains: point
bucket #10 contains:
无序集.htm