Last Updated : 10 Apr, 2024

What will be the output of the below code snippet

map<string, int> mp;
mp.insert(pair<string, int> (\"triangle\", 3));
mp.insert(pair<string, int> (\"square\", 4));
mp.insert(pair<string, int> (\"pentagon\", 5));
mp.insert(pair<string, int> (\"hexagon\", 6));
    
map<string, int> ::iterator itr;
for(itr = mp.begin(); itr != mp.end(); itr++)
     cout << itr->first << \"-\" << itr->second << endl; 

(A) triangle-3
square-4
pentagon-5
hexagon-6
(B) triangle-3
square-4
hexagon-6
pentagon-5
(C) hexagon-6
pentagon-5
square-4
triangle-3
(D) None of these


Answer: (C)

Explanation:
map_name.insert(pair (key, value));
above is also a one way to assign value to corresponding key.
The key/value pair in map are arranged according to natural ordering of keys.


Share your thoughts in the comments