unordered_map::hash_function()是C++ STL中的內置函數,用於獲取哈希函數。該哈希函數是一元函數,僅接受單個參數,並基於該參數返回一個唯一的大小為size_t的值。
用法:
unordered_map_name.hash_function()
參數:該函數不接受任何參數。
返回值:該函數返回哈希函數。
時間複雜度:此函數的時間複雜度為常數O(1)。
以下示例程序旨在說明unordered_map::hash_function()函數。
例子1
// C++ program to illustrate the 
// unordered_map::hash_function() 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_map<string, string> sample; 
  
    // inserts key and elements 
    sample.insert({ "Ankit", "MNNIT" }); 
    sample.insert({ "Ram", "MNNIT" }); 
    sample.insert({ "Manoj", "Trichy" }); 
    sample.insert({ "geeks", "geeks" }); 
  
    // use of hash_function 
    unordered_map<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 << "\n{" << it->first << ":"
             << it->second << "}, "; 
    } 
    return 0; 
}
輸出:
15276750567035005396
Key and Elements:
{geeks:geeks}, 
{Manoj:Trichy}, 
{Ankit:MNNIT}, 
{Ram:MNNIT},
例子2
// C++ program to illustrate the 
// unordered_map::hash_function() 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_map<int, int> sample; 
  
    // inserts key and elements 
    sample.insert({ 1, 5 }); 
    sample.insert({ 2, 6 }); 
    sample.insert({ 3, 6 }); 
    sample.insert({ 4, 7 }); 
  
    // use of hash_function 
    unordered_map<int, int>::hasher fn 
        = sample.hash_function(); 
  
    cout << fn(4) << endl; 
  
    // print the key and elements 
  
    cout << "Key and Elements:"; 
    for (auto it = sample.begin(); it != sample.end(); it++) { 
        cout << "\n{" << it->first << ":"
             << it->second << "}, "; 
    } 
    return 0; 
}
輸出:
4
Key and Elements:
{4:7}, 
{3:6}, 
{1:5}, 
{2:6},
相關用法
- p5.js arc()用法及代碼示例
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ log()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ wcstok()用法及代碼示例
- C++ wcsstr()用法及代碼示例
- C++ real()用法及代碼示例
注:本文由純淨天空篩選整理自ankit15697大神的英文原創作品 unordered_map hash_function() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
