Real-World Applications of HashMaps in C++

Introduction and Goal Setting

Hello there! In this lesson, we will apply HashMaps to real-world challenges. Our focus will be on solving tasks such as cataloging books in a library, counting votes in an election, and tracking inventories.

Real-World Scenarios Calling for HashMaps

HashMaps are beneficial in real-life applications, such as the ones mentioned above, due to their ability to rapidly retrieve data with unique keys and efficiently handle larger datasets. Let's understand their efficiency with some actual examples.

Solving Real-World Task 1: Cataloging Books in a Library

Suppose you're asked to manage the cataloging of books in a library. Here, the book ID serves as the key, while the details of the book, such as the title, author, and year of publication, are stored as values.

This approach allows us to add, search for, and remove books from our library catalog using just a few lines of C++ code.

C++
#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_map<std::string, std::unordered_map<std::string, std::string>> library_catalog;  // Initializing HashMap

    // Details of a book
    std::string book_id = "123";
    std::unordered_map<std::string, std::string> book_details = {{"title", "To Kill a Mockingbird"}, {"author", "Harper Lee"}, {"year_published", "1960"}};

    library_catalog[book_id] = book_details;  // Adding a book to library catalog

    // Searching for a book
    if (library_catalog.find(book_id) != library_catalog.end()) {
        std::cout << "Title: " << library_catalog[book_id]["title"] << ", Author: " << library_catalog[book_id]["author"]
             << ", Year Published: " << library_catalog[book_id]["year_published"] << std::endl;
    }

    library_catalog.erase(book_id);  // Removing a book from library

    return 0;
}

As you can see, HashMaps make the task of cataloging books in the library simpler and more efficient!

Solving Real-World Task 2: Counting Votes in an Election

Imagine a scenario in which we need to count votes in an election. We employ a HashMap, where each name is a unique key, and the frequency of that name serves as the associated value. Let's write some C++ code to better understand this.

C++
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>

int main() {
    std::vector<std::string> votes_list = {"Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"};  // Cast votes
    std::unordered_map<std::string, int> vote_counts;  // Initializing a HashMap

    // Counting the votes
    for (const std::string &name : votes_list) {
        if (vote_counts.find(name) != vote_counts.end()) {  // If name is already a key
            vote_counts[name] += 1;  // increment its value (count)
        } else {  // If name doesn't exist as a key
            vote_counts[name] = 1;  // add it as a key and set value as 1
        }
    }

    for (const auto &pair : vote_counts) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }
    // Prints: Alice: 3, Bob: 2, Charlie: 1

    return 0;
}

HashMaps facilitate the efficient counting of votes.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal