← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 10 of 15

C++ unordered_map

An unordered_map stores key-value pairs like a phone book, but it does not keep them in any order. It finds things very quickly using a hashing trick.
Syntax
cpp
#include <unordered_map>

std::unordered_map<key_type, value_type> map_name;
map_name[key] = value;
map_name.at(key);

What is unordered_map?

std::unordered_map is an associative container storing key-value pairs with no guaranteed order, implemented as a hash table.

Average-case lookup, insertion, and deletion run in O(1) time, faster than map for most workloads that do not need sorted keys.

Example: What is unordered_map?

cpp
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::unordered_map<std::string, int> ages;
	ages["Alex"] = 30;
	// Print to the console with cout
	std::cout << ages["Alex"] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Accessing Elements

Like map, unordered_map supports operator[] and at() for access. Since there is no ordering, elements are retrieved purely by key, not by position.

Example: Accessing Elements

cpp
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::unordered_map<std::string, int> ages = {{"Alex", 30}};
	// Print to the console with cout
	std::cout << ages.at("Alex") << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Hashing & Performance

unordered_map hashes each key to determine its bucket. This gives average O(1) operations but worst-case O(n) if many keys collide. bucket_count() and load_factor() expose the internal hash table state.

Example: Hashing & Performance

cpp
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>

// Program execution starts in main()
int main() {
	std::unordered_map<int, int> m;
	m[1] = 10;
	// Print to the console with cout
	std::cout << "buckets=" << m.bucket_count() << " load_factor=" << m.load_factor() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Iterating (Unordered)

Iterating an unordered_map visits elements in an unspecified, implementation-defined order that can change between insertions. Never rely on iteration order for correctness.

Example: Iterating (Unordered)

cpp
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>

// Program execution starts in main()
int main() {
	std::unordered_map<int, int> m = {{1, 10}, {2, 20}};
	for (const auto &[key, value] : m) {
		// Print to the console with cout
		std::cout << key << ":" << value << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Searching and Erasing

find(), count(), and erase() work the same way as std::map, but rely on hashing rather than tree traversal, making them faster on average for large unsorted datasets.

Example: Searching and Erasing

cpp
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>

// Program execution starts in main()
int main() {
	std::unordered_map<int, int> m = {{1, 10}};
	// Declare it and set it to m.find(1)
	auto it = m.find(1);
	if (it != m.end()) std::cout << "Found: " << it->second << std::endl;
	m.erase(1);
	// Print to the console with cout
	std::cout << m.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting unordered_map to iterate in sorted or insertion order.
  2. Using m[key] to test for existence, which inserts a default entry.
  3. Using a custom key type without providing a hash function and equality.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.