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


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


unordered_multiset::bucket()是C++ STL中的内置函数,该函数返回给定元素所在的存储区编号。铲斗尺寸从0到bucket_count-1不等。

用法:

unordered_multiset_name.bucket(element)

参数:该函数接受一个强制性元素,该元素指定要返回其存储区编号的值。


返回值:它返回一个无符号整数类型,表示元素所在的存储区编号。

以下示例程序旨在说明上述函数:

示例1:

// C++ program to illustrate the 
// unordered_multiset::bucket() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<int> sample; 
  
    // inserts element 
    sample.insert(10); 
    sample.insert(15); 
    sample.insert(15); 
    sample.insert(13); 
    sample.insert(13); 
  
    for (auto it = sample.begin(); it != sample.end(); it++) { 
        cout << "The bucket number in which " << *it 
             << " is " << sample.bucket(*it) << endl; 
    } 
    return 0; 
}
输出:
The bucket number in which 13 is 6
The bucket number in which 13 is 6
The bucket number in which 10 is 3
The bucket number in which 15 is 1
The bucket number in which 15 is 1

示例2:

// C++ program to illustrate the 
// unordered_multiset::bucket() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<char> sample; 
  
    // inserts element 
    sample.insert('a'); 
    sample.insert('a'); 
    sample.insert('b'); 
    sample.insert('b'); 
    sample.insert('c'); 
    sample.insert('c'); 
    sample.insert('e'); 
  
    for (auto it = sample.begin(); it != sample.end(); it++) { 
        cout << "The bucket number in which " << *it 
             << " is " << sample.bucket(*it) << endl; 
    } 
    return 0; 
}
输出:
The bucket number in which e is 16
The bucket number in which a is 12
The bucket number in which a is 12
The bucket number in which b is 13
The bucket number in which b is 13
The bucket number in which c is 14
The bucket number in which c is 14


相关用法


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