'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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vlad Feinstein