Practice mode C++

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.