📜  C++ STL中的unordered_multiset bucket_size()函数

📅  最后修改于: 2021-05-30 13:31:41             🧑  作者: Mango

unordered_multiset :: bucket_size()是C++ STL中的内置函数,该函数返回存储桶中具有元素val的元素数量。它将始终低于bucket_count。存储桶中元素的数量会影响访问存储桶中特定元素所花费的时间

句法:

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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”