📌  相关文章
📜  C++ STL中的unordered_multimap bucket()函数

📅  最后修改于: 2021-05-30 06:38:39             🧑  作者: Mango

unordered_multimap :: bucket()是C++ STL中的内置函数,它返回给定键所在的存储区编号。值区大小从0到bucket_count-1不等。

句法:

unordered_multimap_name.bucket(key)

参数:该函数接受单个强制性参数密钥,该密钥指定要返回其存储区编号的密钥。

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

下面的程序说明了上述函数:

程序1:

// C++ program to illustrate the
// unordered_multimap::bucket() 
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample;
  
    // inserts key and element
    sample.insert({ 10, 100 });
    sample.insert({ 10, 100 });
    sample.insert({ 20, 200 });
    sample.insert({ 30, 300 });
    sample.insert({ 15, 150 });
  
    // iterate for all elements and print its bucket number
    for (auto it = sample.begin(); 
                  it != sample.end(); it++) {
        cout << "The bucket number in which {" 
             << it->first << ", " 
             << it->second << "} is " 
             << sample.bucket(it->first) << endl;
    }
    return 0;
}
输出:
The bucket number in which {15, 150} is 1
The bucket number in which {30, 300} is 2
The bucket number in which {20, 200} is 6
The bucket number in which {10, 100} is 3
The bucket number in which {10, 100} is 3

程式2:

// C++ program to illustrate the
// unordered_multimap::bucket() 
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample;
  
    // inserts key and element
    sample.insert({ 'a', 'b' });
    sample.insert({ 'a', 'b' });
    sample.insert({ 'b', 'c' });
    sample.insert({ 'r', 'a' });
    sample.insert({ 'c', 'b' });
  
    // iterate for all elements and print its bucket number
    for (auto it = sample.begin(); 
                    it != sample.end(); it++) {
        cout << "The bucket number in which {" 
             << it->first << ", "
             << it->second << "} is " 
             << sample.bucket(it->first) << endl;
    }
    return 0;
}
输出:
The bucket number in which {c, b} is 1
The bucket number in which {r, a} is 2
The bucket number in which {b, c} is 0
The bucket number in which {a, b} is 6
The bucket number in which {a, b} is 6
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”