#include <iostream>
#include <unordered_map>
int main() {
// Step 1: Declare an unordered_map with key-value pairs of type int and string
std::unordered_map<int, std::string> myHashMap;
// Step 2: Insert key-value pairs into the unordered_map
myHashMap.insert({1, "One"});
myHashMap.insert({2, "Two"});
myHashMap.insert({3, "Three"});
// Step 3: Access elements using keys
std::cout << "Value at key 2: " << myHashMap[2] << std::endl;
// Step 4: Check if a key exists in the unordered_map
if (myHashMap.find(3) != myHashMap.end()) {
std::cout << "Key 3 exists in the unordered_map" << std::endl;
}
// Step 5: Update the value associated with a key
myHashMap[2] = "New Two";
// Step 6: Iterate through the unordered_map
std::cout << "Iterating through the unordered_map:" << std::endl;
for (const auto& pair : myHashMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
// Step 7: Remove an element by key
myHashMap.erase(1);
// Step 8: Check if the unordered_map is empty
if (myHashMap.empty()) {
std::cout << "The unordered_map is empty" << std::endl;
} else {
std::cout << "The unordered_map is not empty" << std::endl;
}
// Step 9: Get the size of the unordered_map
std::cout << "Size of the unordered_map: " << myHashMap.size() << std::endl;
return 0;
}