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


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