Let's use the following code as an example.
auto cmp = [](ll a, ll b) {
return gcd(12, a) < gcd(12, b);
};
set<ll, decltype(cmp)> s(cmp);
for(ll i = 0; i < 2; i++){
ll x;
cin >> x;
s.insert(x);
cout << "Success! \n";
}
I defined new comparator that compares numbers by their greatest common divisor with 12.
First I successfully inserted 5. And then I tried to insert 7, but it was not successful.
I know that gcd(12, 5) = gcd(12, 7) = 1.
But I am failing to understand how does std::set
check if 5 is equivalent to 7.
It can find out that gcd(12, 7) is not less than gcd(12, 5) using comparator comp I gave it, but how does it figure out that gcd(12, 7) is equal to gcd(12, 5)?
It calls the comparator twice, with different order of arguments.
If cmp(x, y)
and cmp(y, x)
are both false, then x
and y
are considered to be equivalent.