C/C++ Arena

Step 4 of 6

unordered_map

std::unordered_map is a hash table: no ordering, but lookups are usually O(1) instead of O(log n). Same interface as map for [], find, contains.

Classic use: remember things you've seen.

Your turn: write std::pair<int, int> two_sum(const std::vector<int>& v, int target) that returns the indexes {i, j} (with i < j) of two numbers adding up to target, or {-1, -1}. Do it in one pass: for each element, check whether target - v[j] was seen before.

Previous: std::set Next: pair and tuple