🗺️

Maps

 
In C++, std::map is an associative container that stores a collection of key-value pairs, where each key maps to a unique value. In the map, the keys are arranged in sorted order, and the keys are used to access the corresponding values.
Maps are useful in situations where we need to store and access data based on a unique identifier or key. Maps can be used in many real-life applications, such as:
  • Storing user information: A map can be used to store user information, where the key is the user ID, and the value is the user's name, address, phone number, and other relevant information.
  • Dictionary: A map can be used to create a dictionary, where the key is a word, and the value is its definition.
  • Stock market: A map can be used to keep track of stock prices, where the key is the stock symbol, and the value is the price of the stock.
    #include <iostream> #include <map> using namespace std; int main() { // Create a map of strings and integers map<string, int> myMap; // Add elements to the map myMap["A"] = 10; myMap["B"] = 20; myMap["C"] = 30; // Print the elements of the map for (auto it = myMap.begin(); it != myMap.end(); it++) { cout << it->first << ": " << it->second << endl; } // Modify an element in the map myMap["B"] = 25; // Print the modified map cout << "Modified map: " << endl; for (auto it = myMap.begin(); it != myMap.end(); it++) { cout << it->first << ": " << it->second << endl; } // Remove an element from the map myMap.erase("C"); // Print the modified map cout << "Modified map after erasing C: " << endl; for (auto it = myMap.begin(); it != myMap.end(); it++) { cout << it->first << ": " << it->second << endl; } return 0; }
     
    In this implementation, we create a map of strings and integers myMap. We add elements to the map using the subscript operator [], and we print the elements of the map using a for loop and the begin() and end() functions of the map.
    Using the subscript operator, we then modify an element in the map. We print the modified map using a for loop again.
    Using the erase() function of the map, we remove an element from the map, and we print the modified map using a for loop.
    This implementation demonstrates how we can create, modify, and access a map of key-value pairs in C++. It shows how maps can be used to store and access data based on a unique identifier or key, and it provides a real-life example of how maps can be used to keep track of stock prices in the stock market.
     
    IMPLEMENTATION :
     
     
    #include <iostream> #include <map> using namespace std; int main() { // Create a map of string keys and integer values map<string, int> myMap; // Insert elements in the map myMap.insert(pair<string, int>("A", 10)); myMap.insert(pair<string, int>("B", 20)); myMap.insert(pair<string, int>("C", 30)); // Print the elements in the map cout << "Current map elements: " << endl; for (auto it = myMap.begin(); it != myMap.end(); it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Check if a key is present in the map and access its value if (myMap.count("B")) { cout << "B is present in the map and its value is: " << myMap["B"] << endl; } else { cout << "B is not present in the map" << endl; } // Erase an element from the map myMap.erase("B"); // Print the modified map cout << "Modified map after erasing B: " << endl; for (auto it = myMap.begin(); it != myMap.end(); it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Update the value associated with a key myMap["C"] = 35; // Print the modified map cout << "Modified map after updating the value of C to 35: " << endl; for (auto it = myMap.begin(); it != myMap.end(); it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Clear all of the elements of the map myMap.clear(); // Check if the map is empty if (myMap.empty()) { cout << "Map is empty" << endl; } else { cout << "Map is not empty" << endl; } return 0; }
     
    This implementation first creates an empty map of string keys and integer values myMap. It then adds elements to the map using the insert() function. The elements are printed to the console using a for loop and the begin() and end() functions of the map.
    The count() function checks whether a key is present in the map, and the erase() function removes an element from the map. The modified map is printed to the console.
    The [] operator is used to update the value associated with a key, and the modified map is printed to the console again.
    The clear() function deletes all of the elements of the map, and the empty() function checks if the map is empty.
     
     
    PRACTICE QUESTIONS :
    1. Majority Element
    1. Top K Frequent Elements