1#include <unordered_map>
2#include <iostream>
3
4int main()
5{
6 std::unordered_map<std::string, int> age;
7 // Insert
8 age["Michael"] = 16;
9 age.insert(std::pair<std::string, int>{"Bill", 25});
10 age.insert({"Chris", 30});
11
12 // Search and change
13 age["Michael"] = 18;
14 age.at("Chris") = 27;
15
16 // Check if key exists
17 std::string query;
18 query = "Eric";
19 if (age.find(query) == age.end())
20 {
21 std::cout << query << " is not in the dictionary!" << std::endl;
22 }
23
24 // Delete
25 query = "Michael";
26 if (age.find(query) == age.end())
27 {
28 std::cout << query << " is not in the dictionary!" << std::endl;
29 }
30 age.erase(query);
31 if (age.find(query) == age.end())
32 {
33 std::cout << query << " is not in the dictionary!" << std::endl;
34 }
35
36 // Iterate
37 for (const std::pair<std::string, int>& tup : age)
38 {
39 std::cout << "Name: " << tup.first << std::endl;
40 std::cout << "Age: " << tup.second << std::endl;
41 }
42}
43