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


C++ unordered_multimap hash_function()用法及代码示例

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 <iostream> 
#include <unordered_map> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<string, string> 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<string, string>::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 <iostream> 
#include <unordered_map> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<string, string> sample; 
  
    // insertion of key and elements 
    sample.insert({ "gopal", "dave" }); 
    sample.insert({ "geeks", "geeks" }); 
  
    // use of hash_function 
    unordered_multimap<string, string>::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},


相关用法


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