当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ unordered_multiset bucket_size()用法及代码示例


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 <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<int> 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 <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<int> 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


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 unordered_multiset bucket_size() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。