Assuming standard headers and using namespace std, what does this C++ code print? map<string, int> m; m = 3; m = 5; m = 1; for (auto& : m) cout << k;
Correct answer C. afp
Explanation
std::map keeps its elements sorted by key, using std::less (alphabetical order for strings), regardless of insertion order. Iteration therefore visits apple, fig and pear, printing their first letters a, f and p. If insertion order mattered you would need a different structure, and std::unordered_map gives no particular order at all.
Answer checked How we write and check questions
Discussion