📜  C++ STL中的unordered_multimap hash_function()

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

unordered_multimap :: hash_function()是C++ STL中的内置函数,用于获取哈希函数。该哈希函数是一元函数,仅接受单个参数,并基于该参数返回一个唯一的大小为size_t的值。

语法

unordered_multimap_name.hash_function()

参数:该函数不接受任何参数。

返回值:该函数返回哈希函数。

下面的程序说明了unordered_multimap :: hash_function()函数:

程序1

// C++ program to illustrate the
// unordered_multimap::hash_function()
#include 
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample;
  
    // inserts key and elements
    sample.insert({ "gopal", "dave" });
    sample.insert({ "gopal", "dave" });
    sample.insert({ "gopal", "dave" });
    sample.insert({ "geeks", "geeks" });
  
    // use of hash_function
    unordered_multimap::hasher fn
                                      = sample.hash_function();
  
    cout << fn("geeks") << endl;
  
    // print the key and elements
  
    cout << "Key and Elements: ";
    for (auto it = sample.begin(); it != sample.end(); it++) {
        cout << "{" << it->first << ":"
             << it->second << "}, ";
    }
    return 0;
}
输出:
15276750567035005396
Key and Elements: {geeks:geeks}, {gopal:dave}, {gopal:dave}, {gopal:dave},

程序2

// C++ program to illustrate the
// unordered_multimap::hash_function()
#include 
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample;
  
    // insertion of key and elements
    sample.insert({ "gopal", "dave" });
    sample.insert({ "geeks", "geeks" });
  
    // use of hash_function
    unordered_multimap::hasher fn
                                      = sample.hash_function();
  
    cout << fn("geeksforgeeks") << endl;
  
    // print the key and elements
    cout << "Key and Elements: ";
    for (auto it = sample.begin(); it != sample.end(); it++) {
        cout << "{" << it->first << ":"
             << it->second << "}, ";
    }
    return 0;
}
输出:
5146686323530302118
Key and Elements: {geeks:geeks}, {gopal:dave},
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”