Dicionários e Tabelas Hash em C++17: Guia Detalhado
Dictionaries and Hash Tables in C++
What is it and why?
Imagine you have a magic notebook. In it, you can write down a friend's name and their phone number next to it. Then, when you want to call, you just look at the name and immediately see the number! You don't need to flip through all the pages looking for a number and then remember whose it is. The name is the key, and the number is the value.
In programming, such a "notebook" is called a dictionary (or associative array). In C++, the container std::map (also known as "mapping") or std::unordered_map (unordered mapping, works faster but does not store order) is used for this.
Main rule: each key can have only one value. If you write a new number for the same name, the old number disappears.
Basic Syntax
To use a dictionary, you need to include the <unordered_map> (or <map>) library.
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
// Declare a dictionary: key - string, value - string
std::unordered_map<std::string, std::string> phonebook;
// Add entries
phonebook["Anna"] = "+7-999-123-45-67";
phonebook["Boris"] = "+7-888-765-43-21";
phonebook["Vika"] = "+7-777-555-33-11";
// Get value by key
std::cout << "Anna's number: " << phonebook["Anna"] << std::endl;
// Check if key exists in dictionary
if (phonebook.find("Boris") != phonebook.end()) {
std::cout << "Boris is in the phonebook!" << std::endl;
}
// Delete an entry
phonebook.erase("Vika");
// Print all entries (iterate through dictionary)
for (auto& pair : phonebook) {
// pair.first is the key (name)
// pair.second is the value (number)
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
Analysis of Typical Tasks
Task 1. Greeting in Different Languages
We have a dictionary: country → greeting. The user enters a country, the program outputs the greeting.
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<std::string, std::string> greetings;
greetings["Russia"] = "Hello!";
greetings["England"] = "Hello!";
greetings["France"] = "Bonjour!";
greetings["Italy"] = "Ciao!";
std::string country;
std::cout << "Enter country: ";
std::getline(std::cin, country);
// Look for the country in the dictionary
if (greetings.find(country) != greetings.end()) {
std::cout << "Greeting: " << greetings[country] << std::endl;
} else {
std::cout << "Greeting for this country not found." << std::endl;
}
return 0;
}
Task 2. Phonebook (Search)
The phonebook already exists. Need to find a number by name.
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<std::string, std::string> phonebook;
phonebook["Ivan"] = "111-22-33";
phonebook["Maria"] = "444-55-66";
phonebook["Peter"] = "777-88-99";
std::string name;
std::cout << "Enter name to search: ";
std::getline(std::cin, name);
if (phonebook.find(name) != phonebook.end()) {
std::cout << "Phone number: " << phonebook[name] << std::endl;
} else {
std::cout << "Contact not found." << std::endl;
}
return 0;
}
Task 3. Weekly Planner (To-Do List)
Key — day of the week, value — list of tasks (string).
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<std::string, std::string> weekPlan;
weekPlan["Monday"] = "Go to school, do homework";
weekPlan["Tuesday"] = "Football practice";
weekPlan["Wednesday"] = "Clean the room";
weekPlan["Thursday"] = "Help mom with dinner";
weekPlan["Friday"] = "Meet with friends";
std::string day;
std::cout << "Enter day of the week: ";
std::getline(std::cin, day);
if (weekPlan.find(day) != weekPlan.end()) {
std::cout << "Plans for " << day << ": " << weekPlan[day] << std::endl;
} else {
std::cout << "No plans for this day." << std::endl;
}
return 0;
}
Task 4. Digital Notebook (Deleting a Contact)
Delete a contact that is no longer needed.
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<std::string, std::string> contacts;
contacts["Alice"] = "123-45-67";
contacts["Bob"] = "234-56-78";
contacts["Charlie"] = "345-67-89";
std::string name;
std::cout << "Enter contact name to delete: ";
std::getline(std::cin, name);
if (contacts.find(name) != contacts.end()) {
contacts.erase(name);
std::cout << "Contact \"" << name << "\" deleted." << std::endl;
} else {
std::cout << "Contact not found." << std::endl;
}
// Print remaining contacts
std::cout << "Remaining contacts:" << std::endl;
for (auto& pair : contacts) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
Task 5. Dictionary Keys (Product List)
Print all product names (keys) from the dictionary.
#include <iostream>
#include <unordered_map>
#include <