What will be printed as a result of the code execution? (C++11)
#include <iostream>
#include <map>
using namespace std;

int main()
{
    multimap<char, int> mm{{'a',1},{'b',2},{'c',3},{'a',4},{'a',5},{'c',1}};
    map<char, int> m(++mm.begin(), mm.lower_bound('c'));
    for(auto i: m)
        ++i.second;
    for(auto i: m)
        cout << i.second << ' ';
    return 0;
}
Explanation
The elements in mm will be located by key in the initialization order: {'a', 1}, {'a', 4}, {'a', 5}, {'b', 2}, {'c', 3} , {'c', 1}}. Initialization of m starts from the second element and ends before the first element with the key 'c'. So m will contain {'a', 4} and {'b', 2}. In the first loop, the copy of m changes, that is, in the second loop, the unchanged source map will be displayed.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C++ quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes