Using Unordered Maps in C++

In this tutorial, we will explore how to use unordered maps in C++. An unordered map is a data structure that stores elements as key-value pairs and allows for efficient lookup, insertion, and deletion of elements. We will cover the basics of using unordered maps, including declaring and initializing them, inserting and retrieving elements, and using custom hash functions.

Introduction to Unordered Maps

An unordered map in C++ is implemented using a hash table data structure. This means that elements are stored in an array using a hash function that maps keys to indices. The hash function is used to determine the location of each element in the array, allowing for fast lookup and insertion operations.

Declaring and Initializing Unordered Maps

To use an unordered map in C++, you need to include the <unordered_map> header file and declare a variable of type std::unordered_map. Here’s an example:

#include <unordered_map>
#include <string>

int main() {
    // Declare an unordered map with string keys and int values
    std::unordered_map<std::string, int> myMap;

    return 0;
}

Inserting Elements into Unordered Maps

You can insert elements into an unordered map using the insert method or by assigning a value to a key. Here’s an example:

#include <unordered_map>
#include <string>

int main() {
    // Declare an unordered map with string keys and int values
    std::unordered_map<std::string, int> myMap;

    // Insert elements using the insert method
    myMap.insert({"apple", 5});
    myMap.insert({"banana", 10});

    // Insert elements by assigning a value to a key
    myMap["orange"] = 15;

    return 0;
}

Retrieving Elements from Unordered Maps

You can retrieve elements from an unordered map using the at method or by accessing the element directly using its key. Here’s an example:

#include <unordered_map>
#include <string>

int main() {
    // Declare an unordered map with string keys and int values
    std::unordered_map<std::string, int> myMap;

    // Insert elements
    myMap.insert({"apple", 5});
    myMap.insert({"banana", 10});

    // Retrieve elements using the at method
    int value1 = myMap.at("apple");

    // Retrieve elements by accessing directly
    int value2 = myMap["banana"];

    return 0;
}

Using Custom Hash Functions

If you want to use a custom class as a key in an unordered map, you need to define a hash function for that class. Here’s an example:

#include <unordered_map>
#include <string>

// Define a custom class
struct Person {
    std::string name;
    int age;

    // Overload the == operator
    bool operator==(const Person& other) const {
        return name == other.name && age == other.age;
    }
};

// Define a hash function for the Person class
namespace std {
template<>
struct hash<Person> {
    size_t operator()(const Person& p) const {
        // Compute a hash value using the name and age fields
        size_t hashValue = std::hash<std::string>{}(p.name);
        hashValue ^= std::hash<int>{}(p.age);
        return hashValue;
    }
};
}

int main() {
    // Declare an unordered map with Person keys and int values
    std::unordered_map<Person, int> myMap;

    // Insert elements
    myMap.insert({{"John", 30}, 5});
    myMap.insert({{"Jane", 25}, 10});

    return 0;
}

Conclusion

In this tutorial, we have covered the basics of using unordered maps in C++. We have learned how to declare and initialize unordered maps, insert and retrieve elements, and use custom hash functions. With practice and experience, you can become proficient in using unordered maps in your own programs.

Leave a Reply

Your email address will not be published. Required fields are marked *