3

I have a compound data type like:

struct Key {
    optional<int> a;
    optional<int> b;
    optional<int> c;
};

I also have a multiset, multiset<Key>. For example, it contains

{1, 2, 3}
{1, NULL, 3}
{NULL, 2, 3}
{NULL, NULL, 3}

I want to get all the objects in this multiset that match {1, 2, 3}. But there is catch: NULL fields should match with anything. For example, {1, 2, 3} matches with {1, NULL, 3}.

I tried to defined a comparator (<) that ignores NULL values. For example {1, NULL, NULL} == {NULL, 2, 3}. But it does not follow the weak strict ordering and it gives me wrong results.

How can I do that?

Sadjad
  • 1,661
  • 2
  • 13
  • 20

1 Answers1

2

Your problem here is even more serious than not following weak strict ordering rules. Your equality will not even be an equivalence relation: { 1,NULL,3} matches {1, 2, 3} and {1, 4, 3}, but {1, 2, 3} does not match {1, 4, 3}. My conclusion is that you cannot rely on any standard container to meet your matching requirement with catch all values.

If you only want to store them, you should try to use an unordered_set or unordered_multiset because it will allow you to store the value without any problems. But you will have to manually implement a method for searching the container for a match.

Beware: I do not advise you to subclass a standard container (it is generally a wrong idea because they were not designed to allow derivation, have no virtual destructor...), but instead to include one in a custom class and delegate storage to it.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252