0

We have a good explanation for single sets here:

but I wanted to know how this would work for an array of sets like below:

bool cmp(const pair <int, int> &a, const pair <int, int> &b) {
    if (b.first < a.first)
        return true;
    return a < b;
}

set <pair <int, int>, decltype(&cmp)> block[SQ](&cmp);

I prefer method 3 but you can answer any method you like.

  • This is undefined behavior. The comparison function is not a strict weak order. – Sam Varshavchik May 11 '20 at 12:13
  • `set , decltype(&cmp)> block[SQ](&cmp)` is invalid. I guess you meant to call constructor of `set` for each array element, like `set , decltype(&cmp)> block[SQ] = { set , decltype(&cmp)>(&cmp), set , decltype(&cmp)>(&cmp), etc. }` – KamilCuk May 11 '20 at 12:23
  • Yes I meant that; but how can I code that? – CompuPhisics May 11 '20 at 12:51

1 Answers1

0

Something along these lines, perhaps:

struct CmpWrapper {
  bool operator()(const pair <int, int> &a, const pair <int, int> &b) const {
    return cmp(a, b);
  }
};

set <pair <int, int>, CmpWrapper> block[SQ];

As @SamVarshavchik noted in comments, cmp doesn't meet requirements of a strict weak ordering (e.g. there exists pairs p and q where cmp(p, q) and cmp(q, p) are both true). Using it with std::set, directly or via this wrapper, would exhibit undefined behavior.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85