Hash Maps using std::unordered_map

std::unordered_map vs std::map - When to Use Each

When should I use std::unordered_map instead of std::map?

Abstract art representing computer programming

The choice between std::unordered_map and std::map depends on your specific requirements and the characteristics of your data. Here are some guidelines to help you decide when to use each:

Use std::unordered_map when:

  1. You need fast average-case lookup, insertion, and deletion operations. std::unordered_map provides average constant-time complexity (O(1)) for these operations.
  2. The order of elements is not important. std::unordered_map does not maintain any particular order of elements.
  3. You don't need to perform range-based operations or retrieve elements in sorted order.

Use std::map when:

  1. You need to maintain elements in sorted order based on their keys. std::map is an ordered associative container that keeps elements sorted by keys.
  2. You require logarithmic-time complexity (O(log N)) for lookup, insertion, and deletion operations. std::map provides these guarantees.
  3. You need to perform range-based operations, such as finding all elements within a specific range of keys.

Here's an example illustrating a scenario where std::map is preferred:

#include <iostream>
#include <map>
#include <string>

int main() {
  std::map<std::string, int> ageMap{
    {"Alice", 25}, {"Bob", 30},
    {"Charlie", 35}, {"David", 40}};

  auto lower = ageMap.lower_bound("B");  
  auto upper = ageMap.upper_bound("D");  

  for (auto it = lower; it != upper; ++it) {
    std::cout << it->first << ": "
      << it->second << '\n';
Bob: 30
Charlie: 35

In this example, std::map is chosen because we want to retrieve people within a specific range of names (keys). The lower_bound() and upper_bound() functions efficiently find the range of elements based on the sorted order of keys.

On the other hand, if fast lookup is the primary concern and the order of elements is not important, std::unordered_map would be a better choice.

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::string, std::string> phoneBook{
    {"Alice", "1234567890"},
    {"Bob", "9876543210"},
    {"Charlie", "5555555555"}

  // Lookup phone number by name
  std::string name = "Bob";
  auto it = phoneBook.find(name);
  if (it != phoneBook.end()) {
    std::cout << "Phone number of " << name
      << ": " << it->second << '\n';
Phone number of Bob: 9876543210

In this case, std::unordered_map provides fast lookup of phone numbers based on names, which is the primary operation required.

Consider the specific requirements of your application, such as the need for ordering, range-based operations, and the expected performance of different operations, when deciding between std::unordered_map and std::map.

Answers to questions are automatically generated and may not have been reviewed.

Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved