-1

On storing My custom objects on multisets the object's class requires to have a operator< .

I want an explaination of the internal workings of multiset, so that I can understand why operator< is required.

Because, some of objects cannot be compared by < or > operator. Does that means I cannot store them in multiset.

Artaza Sameen
  • 519
  • 1
  • 5
  • 13

2 Answers2

3

It doesn't require < as such, it requires a strict weak ordering.

In short, if the function before is the ordering relation, then the following must hold:

  • For all x, before(x, x) is false
  • For all x and y, if before(x, y) is true then before(y, x) is false
  • For all x, y, and z, if before(x, y) is true and before(y, z) is true, then before(x, z) is true

The < relation is the default because it is already defined for many types, in a way that fulfills the strict weak ordering conditions.
(Exercise: verify that it does.)

The ordering relation is used to establish an equivalence relation between elements; if a is not ordered before b, and b is not ordered before a - that is, !before(a,b) && !before(b, a) - then they are considered equivalent.
Equivalent elements belong to the same "multi-element" of the multiset (note that, unlike with std::set, equivalent elements can occur more than once).
The multiset itself is ordered according to the ordering relation.

Even if you can't define a "strictly less than" relation for a type, you can usually define a "should be ordered 'before' for this particular purpose" relation.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
1

Multiset is a sorted container.

It needs to use a comparator to compare elements.

Using the types operator< is the default, but you can use other comparators, in that case the type of the comparator needs to be passed as template paramter:

template<
    class Key,
    class Compare = std::less<Key>,    // <<----
    class Allocator = std::allocator<Key>
> class multiset;

std::less<Key> is the default and uses Key::operator<.

I want an explaination of the internal workings of multiset, so that I can understand why operator< is required.

Actually no, you don't need that. A multiset is sorted and uses operator< by default because it is specified to do so. If you do care about the implementation you can of course look at it, but it won't give you much insight on why < is used. It is defined in the header <set>.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Under normal circumstances, `<` and `>` would be the only operators that could check for both equality and ordering, no? – super Nov 16 '20 at 08:25
  • @super I dont understand what you mean. `Compare` can be almost anything, the only restriction is that it is a strict weak ordering. – 463035818_is_not_an_ai Nov 16 '20 at 08:26
  • @super containers typically do not use equality but equivalence which is `!(a < b) && !(b < a)` (and thats not necessarily the same as `a == b`) – 463035818_is_not_an_ai Nov 16 '20 at 08:28
  • @super eg `unordered_set` used `==` (equality), which can be a bit surprising when you are used to equivalence via `<`, but as it is unordered it doesn't need `<` and requiring `==` is the lesser constraint – 463035818_is_not_an_ai Nov 16 '20 at 08:30
  • I can't disagree with any of that. Still, it seems to me like there is a good reason to use `<` or `>` as the default. It's intuitive since it would allow odering and *equivalence* checks. – super Nov 16 '20 at 08:32
  • @super didnt say that you disagree, just trying to understand what you mean. You mean: Why not eg `<=` ? That would work too but you'd always need to check `a <= b` and `b <= a` to see if `a < b` – 463035818_is_not_an_ai Nov 16 '20 at 08:33
  • I mean that OP is asking why `operator<` is required. To me it seems there is some what of a reason as to why. – super Nov 16 '20 at 08:37
  • @super I have problems to imagine how else one would sort elements. To know if `a` comes before `b` you look at `a < b`. As the other answer explains, that doesnt have to be the "traditional" `<`, but we can still call it `<` – 463035818_is_not_an_ai Nov 16 '20 at 08:42
  • Exactly. I think an answer could benefit from including that since I don't assume that it is obvious to everyone. – super Nov 16 '20 at 08:43
  • @super thanks for your input, I think I'll try to improve the answer (later) – 463035818_is_not_an_ai Nov 16 '20 at 08:44