-3

My understanding of what unordered_map means is that is stores unit value per key without ordering them. But is it expected that insertion order is not preserved?

When I compile and run:

std::unordered_map<std::string,int> temp;
temp["Start"] = 0;
temp["Read"] = 0;
for ( auto iter : temp )
{
    std::cout << iter.first.c_str();
} 

With VS2015, it outputs

Start
Read

With GCC 4.9 for Android, it outputs:

Read
Start

Is it a bug, or expected?

Rama
  • 3,222
  • 2
  • 11
  • 26
jpo38
  • 20,821
  • 10
  • 70
  • 151

2 Answers2

2

From here:

Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).

I think that pretty much sums it up.

user31601
  • 2,482
  • 1
  • 12
  • 22
1

This is expected. In the standard there're no guarantees regarding the order of elements in std::unordered_map.

alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51