-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table.cpp
More file actions
70 lines (59 loc) · 2.58 KB
/
hash_table.cpp
File metadata and controls
70 lines (59 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <unordered_map> // hash-table
// hash-table in C++ likes dictionary in Python
#include <unordered_set> // group of no repeating elements
// BOOST YOUR EFFICIENCY BY HASH
// hash-table can provide our an effective program
// thanks to its brilliant advantage in Reading and Instertion
int main()
{
std::unordered_map<std::string, int> score; // table
[[maybe_unused]] std::unordered_map<std::string, double> cities{{"Beijing", 852.3}, {"Shanghai", 757.8}, {"Guangzhou", 556.2}};
// LIFO
score["Alice"] = 90;
score["Bob"] = 89;
score["Carter"] = 80;
score.emplace("David", 86); // good efficiency
for(const auto&[candidate,value]:score)
std::cout << candidate << " gets " << value << '\n';
if(score.find("Bob") != score.end())
std::cout << "Bob gets " << score["Bob"] << '\n'; // score.at("Bob")
else
std::cout << "Sorry, we didn't find that.\n";
// Hash-Group
std::unordered_set<int> s;
s.insert(3);
s.insert(5);
s.insert(7);
s.insert(3); // repeat element, no effect
std::cout << "Hash Group Size: " << s.size() << '\n';
for(auto num:s)
std::cout << num << ' ';
std::cout << '\n';
// Vote system (hash-table) -> O(1) finding complexity
std::unordered_map<std::string, int> votes;
votes.reserve(20); // no more than 14 keys
for (std::string candidate{};candidate != "q";)
{
std::cout << "Please enter the candidate's name who you want to vote for (Press `q` to Quit): ";
std::getline(std::cin, candidate);
if(candidate.size() < 2)
break;
if(votes[candidate])
++votes[candidate];
else
votes[candidate] = 1;
}
for(const auto& [candidate,vote]:votes) // extract every bucket in the hash table
std::cout << candidate << ": " << vote << " Vote(s).\n";
std::unordered_map<std::string, int> map{{"Beijing", 1}, {"Shanghai", 2}, {"Guangzhou", 3}, {"Shenzhen", 4}, {"Shanghai",6}};
std::cout << map.count("Shanghai") << '\n'; // ~~~^~~ Invalid Insertion (Override)
std::cout << std::boolalpha << map.contains("Guangzhou") << '\n';
map.at("Beijing") = 10; // valid existed key's value's modification
map.insert({"Shanghai", 8}); // invalid override(ignore)
map.erase("Shanghai");
map.insert({"Shanghai", 6}); // valid insert
for(const auto& [city,num]:map)
std::cout << city << ", " << num << '\n';
return 0;
}