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


C++ unordered_set hash_function()用法及代碼示例

unordered_set::hash_function()是C++ STL中的內置函數,用於獲取哈希函數。該哈希函數是一元函數,僅接受單個參數,並基於該參數返回一個唯一的大小為size_t的值。

用法

unordered_set_name.hash_function()

參數:該函數不接受任何參數。


返回值:該函數返回哈希函數。

以下示例程序旨在說明unordered_set::hash_function()函數:

示例1:

// CPP program to illustrate the 
// unordered_set::hash() function 
  
#include <iostream> 
#include <string> 
#include <unordered_set> 
  
using namespace std; 
  
int main() 
{ 
  
    unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" }; 
  
    // use of hash_function 
    unordered_set<string>::hasher fn = sampleSet.hash_function(); 
  
    cout << fn("geeks") << endl; 
  
    for (auto it = sampleSet.begin(); it != sampleSet.end(); it++) { 
        cout << *it << " "; 
    } 
    cout << endl; 
  
    return 0; 
}
輸出:
15276750567035005396
geeks2 geeks1 for

示例2:

// CPP program to illustrate the 
// unordered_set::hash() function 
  
#include <iostream> 
#include <string> 
#include <unordered_set> 
  
using namespace std; 
  
int main() 
{ 
  
    unordered_set<string> sampleSet; 
  
    // use of hash_function 
    unordered_set<string>::hasher fn = sampleSet.hash_function(); 
  
    cout << fn("geeksforgeeks") << endl; 
  
    return 0; 
}
輸出:
5146686323530302118


相關用法


注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 unordered_set hash_function() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。