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

📅  最后修改于: 2021-05-30 14:18:21             🧑  作者: Mango

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