Avatar ·

Are there advantages of map over unordered_map for simple keys (int, string)?

Hi everyone! I've been writing in C++ for about a year, and I have one question that bothers me. Let's say I need to store a dictionary where the keys are int or string. I know that `std::map` is implemented as a red-black tree (O(log n) per operation), and `std::unordered_map` is implemented as a hash table (O(1) on average). It would seem that for simple keys, `unordered_map` is always faster, and I often see it recommended to beginners. But on StackOverflow I came across a discussion where experienced developers claim that `map` has advantages. I tried writing a small test: ```cpp #include #include #include #include int main() { std::map ordered; std::unordered_map unordered; // Fill with 100000 elements for (int i =