techmore.in

C++ Maps

In C++, std::map is an associative container that stores key-value pairs in a sorted order based on the keys. Each key in a std::map is unique, and the values are associated with these keys. The std::map is implemented as a balanced binary search tree (usually a Red-Black Tree), which ensures that operations such as insertions, deletions, and lookups have logarithmic complexity.

Basic Operations with std::map

  1. Include the Necessary Header:

    cpp
    #include #include
  2. Declare a Map:

    cpp
    std::map<int, std::string> myMap;

    This declares a map where the keys are of type int and the values are of type std::string.

  3. Add Elements:

    cpp
    myMap[1] = "one"; myMap[2] = "two"; myMap[3] = "three";

    You can also use the insert method to add elements:

    cpp
    myMap.insert(std::make_pair(4, "four"));
  4. Access Elements:

    cpp
    std::cout << "Value associated with key 2: " << myMap[2] << std::endl;

    You can also use the at method, which throws an exception if the key does not exist:

    cpp
    try { std::cout << "Value associated with key 5: " << myMap.at(5) << std::endl; } catch (const std::out_of_range& e) { std::cout << "Key 5 does not exist." << std::endl; }
  5. Check for Presence of a Key:

    cpp
    if (myMap.find(3) != myMap.end()) { std::cout << "Key 3 exists in the map." << std::endl; }
  6. Remove Elements:

    cpp
    myMap.erase(2); // Removes the element with key 2
  7. Iterate Through the Map:

    cpp
    for (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << std::endl; }
  8. Get the Size of the Map:

    cpp
    std::cout << "Size of the map: " << myMap.size() << std::endl;
  9. Check if the Map is Empty:

    cpp
    if (myMap.empty()) { std::cout << "Map is empty." << std::endl; }

Custom Comparator

You can specify a custom comparator to change the order of keys in the map:

  1. Define a Custom Comparator:

    cpp
    struct Compare { bool operator()(int a, int b) const { return a > b; // Descending order } };
  2. Declare a Map with the Custom Comparator:

    cpp
    std::map<int, std::string, Compare> customMap;

Example Code

Here's a complete example demonstrating basic operations and a custom comparator:

cpp
#include #include // Custom comparator for descending order struct Compare { bool operator()(int a, int b) const { return a > b; // Descending order } }; int main() { // Default map (ascending order) std::map<int, std::string> myMap; myMap[1] = "one"; myMap[2] = "two"; myMap[3] = "three"; std::cout << "Default map:" << std::endl; for (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << std::endl; } // Map with custom comparator (descending order) std::map<int, std::string, Compare> customMap; customMap[1] = "one"; customMap[2] = "two"; customMap[3] = "three"; std::cout << "Custom map (descending order):" << std::endl; for (const auto& pair : customMap) { std::cout << pair.first << ": " << pair.second << std::endl; } // Check presence and remove elements if (myMap.find(2) != myMap.end()) { std::cout << "Key 2 exists in the default map." << std::endl; } myMap.erase(2); std::cout << "After removing key 2, default map:" << std::endl; for (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; }

Output

yaml
Default map: 1: one 2: two 3: three Custom map (descending order): 3: three 2: two 1: one Key 2 exists in the default map. After removing key 2, default map: 1: one 3: three

This example demonstrates the basic usage of std::map, including iteration, custom ordering, and element management.