Skip to content
Justice Tech School

unordered_map and unordered_set

Hash based containers give fast average lookup but no order.

Example
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <string>

int main() {
  std::unordered_map<std::string, int> m = {{"a", 1}, {"b", 2}};
  m["c"] = 3;
  std::cout << m.size() << " " << m.at("b") << "\n";
  std::unordered_set<int> s = {1, 2, 2, 3};
  std::cout << s.size() << "\n";
  return 0;
}

Result

3 2
3

Exercise

Hash container.

std::unordered<int>

Quiz Test

Lookup speed is?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.