bucket
描述 (Description)
它返回值为k的元素所在的桶号。
声明 (Declaration)
以下是std :: unordered_set :: bucket的声明。
C++11
size_type bucket ( const key_type& k ) const;
参数 (Parameters)
k - 它包含有关桶值的信息。
返回值
它返回值为k的元素所在的桶号。
异常 (Exceptions)
如果任何元素比较对象抛出异常,则抛出异常。
请注意,无效参数会导致未定义的行为。
时间复杂
恒定时间。
例子 (Example)
以下示例显示了std :: unordered_set :: bucket的用法。
#include <iostream>
#include <string>
#include <unordered_set>
int main () {
std::unordered_set<std::string> myset = {"sai","ram","krishna","prasad"};
for (const std::string& x: myset) {
std::cout << x << " is in bucket #" << myset.bucket(x) << std::endl;
}
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
prasad is in bucket #0
krishna is in bucket #2
ram is in bucket #1
sai is in bucket #3