unordered_set :: bucket_size()函数是C++ STL中的内置函数,它返回unordered_set容器中特定存储桶中存在的元素总数。
铲斗处于unordered_set的内部散列表的狭槽,其中元素被存储。
注意:unordered_set中的存储桶编号从0到n-1,其中n是存储桶的总数。
语法:
unordered_set.bucket_size(n);
参数:该函数接受一个必填的单个参数n 。此参数表示要查找元素总数所需的存储桶编号。
返回值:该函数返回存储区n中存在的元素总数。
下面的程序说明了unordered_set :: bucket_size()函数:
程序1 :
CPP
// CPP program to illustrate the
// unordered_set::bucket_size() function
#include
#include
using namespace std;
int main()
{
unordered_set sampleSet;
// to store number of buckets
int bucketCount;
// Inserting elements
sampleSet.insert(5);
sampleSet.insert(10);
sampleSet.insert(15);
sampleSet.insert(20);
sampleSet.insert(25);
bucketCount = sampleSet.bucket_count();
// displaying number of buckets
cout << "sampleSet has " << bucketCount << " buckets\n";
// displaying number of elements in bucket numbered 1
cout << "Bucket number 3 contains "
<< sampleSet.bucket_size(3) << " elements";
return 0;
}
CPP
// CPP program to illustrate the
// unordered_set::bucket_size() function
#include
#include
using namespace std;
int main()
{
unordered_set sampleSet;
// to store number of buckets
int bucketCount;
// Inserting elements
sampleSet.insert("Welcome");
sampleSet.insert("To");
sampleSet.insert("GeeksforGeeks");
sampleSet.insert("Computer Science Portal");
sampleSet.insert("For Geeks");
bucketCount = sampleSet.bucket_count();
// displaying number of buckets
cout << "sampleSet has " << bucketCount << " buckets\n";
// displaying number of elements in bucket numbered 0
cout << "Bucket number 0 contains "
<< sampleSet.bucket_size(0) << " elements";
return 0;
}
输出:
sampleSet has 7 buckets
Bucket number 3 contains 1 elements
程序2 :
CPP
// CPP program to illustrate the
// unordered_set::bucket_size() function
#include
#include
using namespace std;
int main()
{
unordered_set sampleSet;
// to store number of buckets
int bucketCount;
// Inserting elements
sampleSet.insert("Welcome");
sampleSet.insert("To");
sampleSet.insert("GeeksforGeeks");
sampleSet.insert("Computer Science Portal");
sampleSet.insert("For Geeks");
bucketCount = sampleSet.bucket_count();
// displaying number of buckets
cout << "sampleSet has " << bucketCount << " buckets\n";
// displaying number of elements in bucket numbered 0
cout << "Bucket number 0 contains "
<< sampleSet.bucket_size(0) << " elements";
return 0;
}
输出:
sampleSet has 7 buckets
Bucket number 0 contains 0 elements
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。