<unordered_map>
The <unordered_map>
header, introduced in C++11, provides the std::unordered_map
and std::unordered_multimap
container classes. These are associative containers that store key-value pairs with unique keys (for unordered_map
) or multiple keys (for unordered_multimap
). They use hash tables for storage, offering average constant-time complexity for search, insertion, and deletion operations.
unordered_map
(unique keys) and unordered_multimap
(multiple keys allowed)std::unordered_map
: Container with unique keysstd::unordered_multimap
: Container allowing multiple entries with the same key#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<std::string, int> age_map;
// Inserting elements
age_map["Alice"] = 30;
age_map["Bob"] = 25;
age_map.insert({"Charlie", 35});
// Accessing elements
std::cout << "Alice's age: " << age_map["Alice"] << std::endl;
// Checking if a key exists
if (age_map.find("David") == age_map.end()) {
std::cout << "David not found in the map" << std::endl;
}
// Iterating through the map
for (const auto& pair : age_map) {
std::cout << pair.first << " is " << pair.second << " years old" << std::endl;
}
return 0;
}
unordered_map
with string keys and integer values#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_multimap<std::string, std::string> country_capitals;
// Inserting multiple entries for the same key
country_capitals.insert({"USA", "Washington D.C."});
country_capitals.insert({"USA", "New York City"});
country_capitals.insert({"France", "Paris"});
country_capitals.insert({"Japan", "Tokyo"});
// Counting entries for a key
std::cout << "USA has " << country_capitals.count("USA") << " entries" << std::endl;
// Finding all entries for a key
auto range = country_capitals.equal_range("USA");
for (auto it = range.first; it != range.second; ++it) {
std::cout << "USA: " << it->second << std::endl;
}
return 0;
}
unordered_multimap
to store multiple values for the same keyequal_range
#include <iostream>
#include <unordered_map>
#include <string>
struct Point {
int x, y;
bool operator==(const Point& other) const {
return x == other.x && y == other.y;
}
};
namespace std {
template <>
struct hash<Point> {
size_t operator()(const Point& p) const {
return hash<int>()(p.x) ^ (hash<int>()(p.y) << 1);
}
};
}
int main() {
std::unordered_map<Point, std::string> point_map;
point_map[{1, 2}] = "Point A";
point_map[{3, 4}] = "Point B";
for (const auto& pair : point_map) {
std::cout << "Point (" << pair.first.x << ", " << pair.first.y << "): "
<< pair.second << std::endl;
}
return 0;
}
unordered_map
Point
structunordered_map
typically offers better average-case performance for lookups compared to std::map
, but may use more memoryThe <unordered_map>
header in C++ provides hash table-based associative containers that offer fast average-case performance for key-based operations. std::unordered_map
and std::unordered_multimap
are useful when you need quick lookups and don't require the elements to be sorted.
unordered_map
stores unique key-value pairs, while unordered_multimap
allows multiple values per keyBy using unordered_map
or unordered_multimap
, C++ developers can implement efficient key-value storage and retrieval mechanisms, especially in situations where fast lookups are crucial and the order of elements is not important.