2019-01-28 第四天 (#217, #219)

因为#217不能傻乎乎地像#299那样自制Hash Table了(因为涉及到hashing值的算法),这里也算是对C++ STL自带的两种Hash Table数据结构unordered_setunordered_map做一个梳理。

unordered_set
这个是最简单的Hash Table,只能知道对应key值在table中是否存在,也就是说key值没有对应的value。
Geeksforgeeks 上的例子:

// C++ program to demonstrate various function of unordered_set 
#include  
using namespace std; 

int main() 
{ 
    // declaring set for storing string data-type 
    unordered_set stringSet; 

    // inserting various string, same string will be stored 
    // once in set 
    stringSet.insert("code"); 
    stringSet.insert("in"); 
    stringSet.insert("c++"); 
    stringSet.insert("is"); 
    stringSet.insert("fast"); 

    string key = "slow"; 

    //   find returns end iterator if key is not found, 
    // else it returns iterator to that key 
    if (stringSet.find(key) == stringSet.end()) 
        cout << key << " not found\n\n"; 
    else
        cout << "Found " << key << endl << endl; 

    key = "c++"; 
    if (stringSet.find(key) == stringSet.end()) 
        cout << key << " not found\n"; 
    else
        cout << "Found " << key << endl; 

    // now iterating over whole set and printing its 
    // content 
    cout << "\nAll elements : "; 
    unordered_set :: iterator itr; 
    for (itr = stringSet.begin(); itr != stringSet.end(); itr++) 
        cout << (*itr) << endl; 
} 

最重要的几个操作:

unorder_set