Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't unordered associative containers implement the less than operator?

Tags:

c++

std

c++11

stl

The unordered associative containers unordered_set, unordered_map etc do not have a less than operator operator<, neither as a member function nor as a free function. Why? There is no specialization of less either. SGI STL's hash_* types also lack this feature.

If we exclude the concept of underlying types, all container types fulfil the requirements of regular types as defined e.g. in Stepanov & McJones' Elements of Programming. The sole exception are the unordered associative types, which lack operator<.


I could not come up with an efficient implementation of such an operator that is consistent with the given definition of equality, so might efficiency be the reason? On the other hand, operator== for unordered associative containers in some cases needs to rehash every element of one container and look it up in the other - O(N) average, but worst case O(N²).

like image 814
dyp Avatar asked May 18 '15 00:05

dyp


People also ask

What is the primary difference between the ordered and unordered associative containers?

7 Answers. Show activity on this post. The main difference is that if you iterate through an ordered container by incrementing the iterator, you'll visit the elements of the container in the order of the keys. That doesn't necessarily hold for unordered containers.

Which of the following is unordered associative container?

The four unordered associative containers are called unordered_set , unordered_multiset , unordered_map , and unordered_multimap .

Can two unordered maps be compared?

The comparison between unordered_map objects is not affected by the arbitrary order in which they store their elements. Two unordered_maps are equal if they have the same number of elements and the elements in one container are a permutation of the elements in the other container. Otherwise, they are unequal.

How many associative containers are provided?

How many Associative Containers are provided by C++? Explanation: C++ provides 4 types of Associative Containers namely Set, Map, multiset and multimap.


1 Answers

Conceptually it doesn't make a lot of sense. Consider a bag of different colored marbles. And lets say you have two bags of marbles:

  1. Contains red, blue, green.
  2. Contains purple, red, yellow.

Is bag 1 < bag 2?

Even if you associate an order to the colors, say:

red < yellow < purple < blue < green

It is still difficult to say if one bag is less than than the other because internally the bag associates no order to the marbles. That is bag 1 could be output in any of 6 formats, which are all equivalent:

  1. red, blue, green
  2. red, green, blue
  3. blue, red, green
  4. blue, green, red
  5. green, red, blue
  6. green, blue, red

None of the above six are less than any of the other. Indeed, they are all equal, no matter whether red < blue or blue < red. A bag is not a sequence ... it is an unordered sequence.

Historically the unordered containers also did not have equality operators. However it does make sense to ask if one bag contains the same colored marbles as another bag. And here the issue is one of efficiency. Eventually algorithms and proposals were presented to sway the committee to add equality comparisons for the unordered containers:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3068.pdf

like image 86
Howard Hinnant Avatar answered Sep 27 '22 20:09

Howard Hinnant