Are ordered elements guaranteed in std :: map?

Is this just a side effect of the implementation (red-black tree) or is the order guaranteed by the C ++ standard?

+5
source share
4 answers

Ordered iteration is not an implementation detail; It is guaranteed by the C ++ standard. This is a fundamental property of all associative containers (C ++ 03 §23.1.2 / 9):

The fundamental property of associative container iterators is that they iterate over containers in non-decreasing order of keys, where non-descent is determined by the comparison that was used to create them. For any two dereferenced iterators iand jsuch that the distance from ito is jpositive,

    value_comp(*j, *i) == false

value_comp- this is the comparator with which the map was built (by default this std::less<T>).

+11
source

§23.1.2 / 2:

Key Compare, (25.3) Key.... Compare . .

Compare - , std::less<Key>.

. , .

- . 23.1.2/10 23.1.2/9 ( ) , / / / begin end.

+4

It is guaranteed by the C ++ standard.

+2
source

Guaranteed. If you want something that is not limited to this, try boost :: unordered_map <>

0
source

All Articles