C++ STL 中 unordered_multiset::bucket_size() 函数
unordered_multiset::bucket_size() 是 C++ STL 中的内置函数,它返回具有元素 val 的桶中的元素数。它始终低于桶计数。桶中的元素数量影响访问桶中特定元素所需的时间。
语法:
unordered_multiset_name.bucket_size(val)
参数: 该函数接受一个参数 val ,指定要返回其桶大小的元素。
返回值: 它返回一个无符号整数类型,表示具有元素 val 的桶中的元素数。
以下是上述函数的演示程序:
程序1:
// C++ program to illustrate the
// unordered_multiset::bucket_size() function
#include
using namespace std;
int main()
{
// declaration
unordered_multiset sample;
// inserts element
sample.insert(11);
sample.insert(11);
sample.insert(11);
sample.insert(12);
sample.insert(13);
sample.insert(13);
sample.insert(14);
for (auto it = sample.begin(); it != sample.end(); it++) {
cout << "The bucket size in which " << *it
<< " is " << sample.bucket_size(*it) << endl;
}
return 0;
}
The bucket size in which 14 is 1
The bucket size in which 11 is 3
The bucket size in which 11 is 3
The bucket size in which 11 is 3
The bucket size in which 12 is 1
The bucket size in which 13 is 2
The bucket size in which 13 is 2
程序2:
// C++ program to illustrate the
// unordered_multiset::bucket_size() function
#include
using namespace std;
int main()
{
// declaration
unordered_multiset sample;
// inserts element
sample.insert(1);
sample.insert(1);
sample.insert(1);
sample.insert(2);
sample.insert(3);
sample.insert(4);
sample.insert(3);
for (auto it = sample.begin(); it != sample.end(); it++) {
cout << "The bucket size in which " << *it
<< " is " << sample.bucket_size(*it) << endl;
}
return 0;
}
The bucket size in which 1 is 3
The bucket size in which 1 is 3
The bucket size in which 1 is 3
The bucket size in which 2 is 1
The bucket size in which 3 is 2
The bucket size in which 3 is 2
The bucket size in which 4 is 1