std:set如何在插入时检查set中是否存在等价元素?

How does std:set check if there is an equivalent element in set during the insertion?

我们以下面的代码为例。

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";
}

我定义了新的比较器,它将数字的最大公约数与 12 进行比较。

先成功插入5,然后尝试插入7,但没有成功。

我知道 gcd(12, 5) = gcd(12, 7) = 1。

但我不明白 std::set 如何检查 5 是否等于 7。

用comparator comp可以查出gcd(12, 7)不小于gcd(12, 5) 我给了,可是怎么查出来的gcd(12, 7) 等于 gcd(12, 5)?

它调用比较器两次,参数顺序不同。

如果cmp(x, y)cmp(y, x)都是假的,那么xy被认为是等价的。