Sathwick
Sathwick

Reputation: 307

unordered_map and unordered_set are working differently in MSVC++ and G++ compilers

unordered_map and unordered_set are working differently in MSVC++ and G++ compilers. The insertion order is maintained properly in MSVC++ but not in g++

unordered_set<char> uset;
uset.emplace('b');
uset.emplace('a');
uset.emplace('d');
uset.emplace('c');
for (auto it : uset) {
    cout << it << " ";
}

The above code prints b a d c on windows with MSVC++ where as prints c d b a on linux using GCC(g++). Which is correct? I am a windows dev for long time, just surprised looking at results on linux.

Upvotes: 1

Views: 211

Answers (1)

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

According to https://en.cppreference.com/w/cpp/container/unordered_set , the order depends on the hash function, that might be different in different implementations.

Upvotes: 5

Related Questions