0Pricing
C++ Academy · Lesson

std::unordered_map

Fast hash-based lookup.

std::unordered_map is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is unordered_map?

std::unordered_map stores key-value pairs in a hash table. Average lookup, insert, and erase are constant time, but elements have no sorted order.

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> ages;
    ages["Alice"] = 30;
    ages["Bob"] = 25;
    std::cout << ages["Alice"] << '\n';
    return 0;
}

map vs unordered_map

Choose based on needs:

  • map: sorted, O(log n) operations.
  • unordered_map: unordered, O(1) average operations.

Use unordered_map when you only need fast lookups.

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<int, std::string> m{{3, "c"}, {1, "a"}, {2, "b"}};
    std::cout << m.size() << " entries (order not guaranteed)\n";
    return 0;
}

Inserting and Updating

The same API as map: use [], insert(), or emplace().

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> m;
    m["x"] = 1;
    m.insert({"y", 2});
    m.emplace("z", 3);
    std::cout << m.size() << '\n';
    return 0;
}

Lookup with find

find() returns an iterator or end(). This avoids accidentally inserting a default value the way [] would.

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> m{{"a", 1}};
    auto it = m.find("a");
    if (it != m.end()) std::cout << it->second << '\n';
    std::cout << "size: " << m.size() << '\n';
    return 0;
}

Checking Existence

count() returns 0 or 1, and C++20 adds contains() for a clear boolean check.

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> m{{"key", 99}};
    std::cout << m.count("key") << '\n';
    std::cout << m.count("missing") << '\n';
    return 0;
}

Iterating

You can iterate, but the order is unspecified. Never rely on it being sorted or insertion-ordered.

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> m{{"a", 1}, {"b", 2}, {"c", 3}};
    int total = 0;
    for (const auto& [k, v] : m) total += v;
    std::cout << "sum = " << total << '\n';
    return 0;
}

Erasing

erase() removes by key and returns the number removed.

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> m{{"a", 1}, {"b", 2}};
    m.erase("a");
    std::cout << m.count("a") << ' ' << m.size() << '\n';
    return 0;
}

Counting Frequencies Fast

For large datasets where order doesn't matter, unordered_map counts frequencies faster than map.

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

int main() {
    std::string items[] = {"a", "b", "a", "c", "b", "a"};
    std::unordered_map<std::string, int> freq;
    for (const auto& s : items) freq[s]++;
    std::cout << "a appears " << freq["a"] << " times\n";
    return 0;
}

Using at()

at() returns a reference and throws std::out_of_range for a missing key, never inserting.

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> m{{"score", 42}};
    std::cout << m.at("score") << '\n';
    return 0;
}

Default Insertion via []

Accessing a missing key with [] inserts it with a value-initialized value (0 for ints). Be careful, it silently grows the map.

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> m;
    std::cout << m["new"] << '\n';
    std::cout << "size: " << m.size() << '\n';
    return 0;
}

Clearing the Map

clear() empties the table, and empty() reports whether it has no elements.

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<int, int> m{{1, 1}, {2, 2}};
    m.clear();
    std::cout << std::boolalpha << m.empty() << '\n';
    return 0;
}

Quick Check

Test your understanding of unordered_map ordering.

Recap

You learned that std::unordered_map:

  • uses a hash table for O(1) average operations
  • has no guaranteed order
  • shares the same API as map (find, count, at, erase)

Next, you'll see unordered_set for fast unique-element storage.

Frequently asked questions

Is the “std::unordered_map” lesson free?

Yes — the full text of “std::unordered_map” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “std::unordered_map”?

Fast hash-based lookup. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “std::unordered_map” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. std::unordered_map
  2. unordered_set
  3. Custom Hash Functions
  4. Performance Considerations
← Back to C++ Academy