Avatar ·

How to properly iterate over all elements of std::map in C++?

Hello everyone! I recently started learning C++ and encountered a task: I need to go through all key-value pairs in std::map and, for example, print them to the screen or perform some operation. I understand that map is an associative container where each element is stored as std::pair, but I can't figure out the loop syntax. I tried writing this: ```cpp #include #include #include int main() { std::map ages; ages["Alice"] = 30; ages["Bob"] = 25; ages["Charlie"] = 35; // My first attempt — this is clearly wrong for (int i = 0; i < ages.size(); i++) { std::cout << ages[i] << std::endl; // compilation error } } ``` But this, of course, doesn't work because operator[] for