當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。