Unordered Maps

Unordered Maps

 
In C++, std::unordered_map is an associative container that stores a collection of key-value pairs, where each key maps to a unique value. Unordered maps differ from maps in that the keys in unordered maps are not ordered, but rather hashed to create a unique index in constant time to access the corresponding value.
Unordered maps are useful in situations where we need fast access to key-value pairs and the order of the keys is not important. They have an average constant time O(1) complexity for many operations, such as insertion, deletion, and searching, although worst-case time complexity depends on the hash function used.
 
 
#include <iostream> #include <unordered_map> using namespace std; int main() { // Create an unordered map of string keys and integer values unordered_map<string, int> myUnorderedMap; // Insert elements in the unordered map myUnorderedMap.insert(make_pair("A", 10)); myUnorderedMap.insert(make_pair("B", 20)); myUnorderedMap.insert(make_pair("C", 30)); // Print the elements in the unordered map cout << "Current unordered map elements: " << endl; for (auto it = myUnorderedMap.begin(); it != myUnorderedMap.end(); it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Check if a key is present in the unordered map and access its value if (myUnorderedMap.count("B")) { cout << "B is present in the unordered map and its value is: " << myUnorderedMap["B"] << endl; } else { cout << "B is not present in the unordered map" << endl; } // Erase an element from the unordered map myUnorderedMap.erase("B"); // Print the modified unordered map cout << "Modified unordered map after erasing B: " << endl; for (auto it = myUnorderedMap.begin(); it != myUnorderedMap.end(); it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Update the value associated with a key myUnorderedMap["C"] = 35; // Print the modified unordered map cout << "Modified unordered map after updating the value of C to 35: " << endl; for (auto it = myUnorderedMap.begin(); it != myUnorderedMap.end(); it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Clear all of the elements of the unordered map myUnorderedMap.clear(); // Check if the unordered map is empty if (myUnorderedMap.empty()) { cout << "Unordered map is empty" << endl; } else { cout << "Unordered map is not empty" << endl; } return 0; }
This implementation creates an empty unordered map of string keys and integer values myUnorderedMap. It then adds elements to the unordered map using the insert() function. The elements are printed to the console using a for loop and the begin() and end() functions of the unordered map.
The count() function checks whether a key is present in the unordered map, and the erase() function removes an element from the unordered map. The modified unordered map is printed to the console.
The [] operator is used to update the value associated with a key, and the modified unordered map is printed to the console again.
The clear() function deletes all of the elements of the unordered map, and the empty() function checks if the unordered map is empty. Finally, the program returns 0.
 
 
PRACTICE QUESTIONS :
  1. Letter Combinations of a Phone Number
  1. Roman to Integer