What will be printed by this program if you enter 3 values from the console: 1, 2, and 3?
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
    string s;
    map<string, int> m;

    while (cin >> s) {
        m[s] = m.size();
    }

    for (map<string, int>::iterator i = m.begin(); i != m.end(); ++i) {
        cout << i->second << " ";
    }

    return 0;
}
Explanation
The C ++ standard does not specify the calculation order of the operands of the assignment operator, so the result may be different (depends on the compiler): "0 1 2" or "1 2 3". This is determined by what operation will be performed first: determining the size of the map or adding a new element to the map. The correct answer is "the result is not defined".

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести