#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> myMap;
// Step 2: Insert key-value pairs into the unordered_map using the insert() function
myMap.insert({1, "One"});
myMap.insert({2, "Two"});
myMap.insert({3, "Three"});
// Step 3: Access elements using the square bracket notation
std::cout << "Value at key 2: " << myMap[2] << std::endl;
// Step 4: Check if a key exists using the find() function
auto it = myMap.find(3);
if (it != myMap.end()) {
std::cout << "Key 3 found. Value: " << it->second << std::endl;
} else {
std::cout << "Key 3 not found." << std::endl;
}
// Step 5: Erase an element by key using the erase() function
myMap.erase(1);
// Step 6: Iterate through the unordered_map using iterators
std::cout << "Iterating through the map:" << std::endl;
for (auto& pair : myMap) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}
// Step 7: Check if the unordered_map is empty
if (myMap.empty()) {
std::cout << "The map is empty." << std::endl;
} else {
std::cout << "The map is not empty." << std::endl;
}
// Step 8: Get the size of the unordered_map
std::cout << "Size of the map: " << myMap.size() << std::endl;
return 0;
}