. Advertisement .
..3..
. Advertisement .
..4..
A HashMap, so-called a hash table, is a critical data structure that maps keys to values. As such, it employs a hash function to generate an index into an array of slots or buckets where the matching data may be retrieved from.
The question here is: How do we make the most out of such a helpful structure?
Jump right in to learn more on HashMap C++ and approaches to get your data implemented right!
How To Use HashMap C++
Use HashMap With std::unordered_map in C++
std::unordered map is similar to Python’s dict, with the exception that the type of key and value in std::unordered map must be the same for all keys and values.
For example:
#include <unordered_map>
#include <iostream>
int main()
{
std::unordered_map<std::string, int> age;
// Insert
age["Michael"] = 16;
age.insert(std::pair<std::string, int>{"Bill", 25});
age.insert({"Chris", 30});
// Search and change
age["Michael"] = 18;
age.at("Chris") = 27;
// Check if key exists
std::string query;
query = "Eric";
if (age.find(query) == age.end())
{
std::cout << query << " is not in the dictionary!" << std::endl;
}
// Delete
query = "Michael";
if (age.find(query) == age.end())
{
std::cout << query << " is not in the dictionary!" << std::endl;
}
age.erase(query);
if (age.find(query) == age.end())
{
std::cout << query << " is not in the dictionary!" << std::endl;
}
// Iterate
for (const std::pair<std::string, int>& tup : age)
{
std::cout << "Name: " << tup.first << std::endl;
std::cout << "Age: " << tup.second << std::endl;
}
}
Use HashMap With std::map in C++
The element of std::map is stored in mapped key-value pairs, which makes it an associative container. The key in std::map and std::unordered map should always be unique. However, there might be several unique keys with the same mapped value.
The Self-Balancing Binary Search Tree (AVL tree/ Red-black tree) is used to implement std::map. The key characteristic of BST (Self-Balancing Binary Search Tree) is that as components are added or removed, the height is automatically kept to a minimum.
The average time complexity of an operation in std::map varies logarithmically on the number of elements being stored; hence, an operation takes O(log n) time on average. O(log n) denotes the Big O notation, and n is the number of variables.
For Example:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
map<int, string> Players;
Players.insert(std::pair<int, string>(2, "Lin Dan"));
Players.insert(std::pair<int, string>(1, "Chen Long"));
cout << "Number of Players " << Players.size() << endl;
for (map<int, string>::iterator it = Players.begin(); it != Players.end(); ++it) {
cout << (*it).first << ": " << (*it).second << endl;
}
}
Output:
Number of Players 2
1: Chen Long
2: Lin Dan
Conclusion
The above information is all you need to know about a HashMap C++. Now is your chance to get out on the road and test it for yourself. Don’t forget to leave a comment if you still have any questions. See then!
Leave a comment