目录

bucket_size

描述 (Description)

它返回桶n中的元素数。

声明 (Declaration)

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

C++11

size_type bucket_size ( size_type n ) const;

参数 (Parameters)

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

返回值

它返回桶n中的元素数。

异常 (Exceptions)

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

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

时间复杂

恒定时间。

例子 (Example)

以下示例显示了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.
↑回到顶部↑
WIKI教程 @2018