Lesson 4

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++
1#include <iostream> 2#include <unordered_map> 3#include <string> 4 5int main() { 6 std::unordered_map<std::string, std::unordered_map<std::string, std::string>> library_catalog; // Initializing HashMap 7 8 // Details of a book 9 std::string book_id = "123"; 10 std::unordered_map<std::string, std::string> book_details = {{"title", "To Kill a Mockingbird"}, {"author", "Harper Lee"}, {"year_published", "1960"}}; 11 12 library_catalog[book_id] = book_details; // Adding a book to library catalog 13 14 // Searching for a book 15 if (library_catalog.find(book_id) != library_catalog.end()) { 16 std::cout << "Title: " << library_catalog[book_id]["title"] << ", Author: " << library_catalog[book_id]["author"] 17 << ", Year Published: " << library_catalog[book_id]["year_published"] << std::endl; 18 } 19 20 library_catalog.erase(book_id); // Removing a book from library 21 22 return 0; 23}

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++
1#include <iostream> 2#include <unordered_map> 3#include <vector> 4#include <string> 5 6int main() { 7 std::vector<std::string> votes_list = {"Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"}; // Cast votes 8 std::unordered_map<std::string, int> vote_counts; // Initializing a HashMap 9 10 // Counting the votes 11 for (const std::string &name : votes_list) { 12 if (vote_counts.find(name) != vote_counts.end()) { // If name is already a key 13 vote_counts[name] += 1; // increment its value (count) 14 } else { // If name doesn't exist as a key 15 vote_counts[name] = 1; // add it as a key and set value as 1 16 } 17 } 18 19 for (const auto &pair : vote_counts) { 20 std::cout << pair.first << ": " << pair.second << std::endl; 21 } 22 // Prints: Alice: 3, Bob: 2, Charlie: 1 23 24 return 0; 25}

HashMaps facilitate the efficient counting of votes.

Solving Real-World Task 3: Tracking Store Inventories

Finally, consider a task that involves managing a store's inventory. Here, we can use a HashMap in which product names are keys and quantities are values. This approach allows us to easily add new items, adjust the quantity of items, check whether an item is in stock, and much more.

C++
1#include <iostream> 2#include <unordered_map> 3#include <string> 4 5int main() { 6 std::unordered_map<std::string, int> store_inventory = {{"Apples", 100}, {"Bananas", 80}, {"Oranges", 90}}; // Initializing an inventory 7 8 store_inventory["Pears"] = 50; // Adding a new product to inventory and setting its quantity 9 10 store_inventory["Apples"] += 20; // Updating number of apples in inventory 11 12 std::string prod = "Apples"; // A product to be checked 13 std::cout << "Total " << prod << " in stock: " << store_inventory[prod] << std::endl; 14 15 // Check if a product is in stock 16 prod = "Mangoes"; 17 if (store_inventory.find(prod) != store_inventory.end()) { // If mangoes exist in inventory 18 std::cout << prod << " are in stock." << std::endl; 19 } else { // If mangoes don't exist in inventory 20 std::cout << prod << " are out of stock." << std::endl; 21 } 22 23 return 0; 24}

Thus, when managing inventory data, HashMaps offer an efficient solution!

Lesson Summary and Practice

In this lesson, we bridged the gap between the theory of HashMaps and their practical applications. We explored real-world problems that can be solved using HashMaps and implemented C++ code to address them.

Now, get ready for hands-on practice exercises that will help reinforce these concepts and hone your HashMap problem-solving skills. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.