【问题标题】:Find key in std::set with custom comparator使用自定义比较器在 std::set 中查找键
【发布时间】:2021-10-23 06:17:04
【问题描述】:

我有一个名为Example 的类,其中包含两个整数元素a 和b,使用该类创建了一个set,并自定义了comparator。 comparator 使用第二个变量b 作为更高的优先级,按降序排列。如果该值相同,则第一个变量将具有较高值的​​优先级。这是我的代码:

[C++ 14 ISO]

#include <iostream>
#include <set>

using namespace std;

class Example
{
    public:
        int a;
        int b;
        Example(int x, int y) { a=x; b=y; }
};

class Comparator
{
    public:
        using is_transparent = true_type;

        bool operator()(const int &left, const Example& right) const
        {
            if(left > right.b) return true;
            if(left == right.b) return left > right.a ? true: false;

            return false;
        }

        bool operator()(const Example& left, const int& right) const
        {
            if(left.b > right) return true;
            if(left.b == right) return left.a > right ? true: false;

            return false;
        }

        bool operator()(const Example& left, const Example& right) const
        {
            if(left.b > right.b) return true;
            if(left.b == right.b) return left.a > right.a ? true: false;

            return false;
        }
};

int main()
{
    set<Example, Comparator> sa;
    sa.emplace(Example(4,3));
    sa.emplace(Example(1,2));
    sa.emplace(Example(5,3));
    sa.emplace(Example(2,7));
    sa.emplace(Example(3,1));

    for(auto it=sa.begin(); it!=sa.end(); it++)
        cout<< it->a <<" "<< it->b<<endl;

    cout<<"Find: "<<endl;
    auto it=sa.find(3);
    if(it!=sa.end())
        cout << it->a<<" " << it->b << endl;
    return 0;
}

输出:

2 7
5 3
4 3
1 2
3 1
Find:

使用上述比较器找不到值3。 有人告诉我,在使用自定义比较器的情况下,如何使用set 数据在Example 类中找到变量a 的第一个值?

当我使用下面的比较器时,与仅第一个变量a相比,按升序和降序排序时效果很好。

class Comparator
{
    public:
        using is_transparent = true_type;

        bool operator()(const int & left, const Example& right) const
        {
            return left < right.a;
        }

        bool operator()(const Example & left, const int& right) const
        {
            return left.a < right;
        }

        bool operator()(const Example& left, const Example& right) const
        {
            return left.a < right.a;
        }
};

输出:

1 2
2 7
3 1
4 3
5 3
Find:
3 1

【问题讨论】:

  • 3 将等同于带有您的比较器的Example(3, 3),它在集合中不存在。
  • 顺便说一句 return left &gt; right.a ? true: false; 只是 return left &gt; right.a;
  • 你可以使用 std::find_if 但可能 std::set 是错误的容器
  • @463035818_is_not_a_number 是的,这里只有关系运算符足以用于布尔输出。我认为std::find_if 的复杂度与 O(n) 呈线性关系。

标签: c++ stl set comparator


【解决方案1】:

此实现无法工作。 2个比较器功能与左侧或右侧int ,比较有误。

您只能比较一个值。与 2 个值进行比较是没有意义的。只有一个。

“查找”算法将从两端进行比较以找出值是否相等。

为了在集合中插入一个新的示例,只会使用bool operator()(const Example&amp; left, const Example&amp; right) const。

要查找带有std::find 的值,首先会调用bool operator()(const Example&amp; left, const int&amp; right) const,然后会为同一对调用bool operator()(const int&amp; left, const Example&amp; right) const。

if(left.b == right) return left.a &gt; right ? true: false; 在第一种情况下总是会导致错误的结果。而且,对于第二种情况也是如此。这里if(left &gt; right.b) return true; 已经会导致错误的结果。


只需像这样修改您的代码:

#include <iostream>
#include <set>

using namespace std;

class Example
{
public:
    int a;
    int b;
    Example(int x, int y) { a = x; b = y; }
};

class Comparator
{
public:
    using is_transparent = true_type;

    bool operator()(const int& left, const Example& right) const
    {
        if (left > right.b) return true;
        return false;
    }

    bool operator()(const Example& left, const int& right) const
    {
        if (left.b > right) return true;
        return false;
    }

    bool operator()(const Example& left, const Example& right) const
    {
        if (left.b > right.b) return true;
        if (left.b == right.b) return left.a > right.a ? true : false;
        return false;
    }
};

int main()
{
    set<Example, Comparator> sa;
    sa.emplace(Example(4, 3));
    sa.emplace(Example(1, 2));
    sa.emplace(Example(5, 3));
    sa.emplace(Example(2, 7));
    sa.emplace(Example(3, 1));

    for (auto it = sa.begin(); it != sa.end(); it++)
        cout << it->a << " " << it->b << endl;

    cout << "Find: " << endl;
    auto it = sa.find(3);
    if (it != sa.end())
        cout << it->a << " " << it->b << endl;
    return 0;
}

【讨论】:

  • 请注意,这将返回带有b==3 的some 元素,不一定是第一个(a 最大的元素)。所以不是 OP 要求的,我认为:“我怎样才能找到第一个值”
  • @IgorTandetnik 是的,正确的。客户比较器和std::set 没有其他方法。使用函数(或 Lambda)很容易实现。
  • @ArminMontigny 这个关于操作符方法的解释现在对我来说很清楚了。如果数据按b排序但找到a的值的键,是否有可能?
【解决方案2】:

进行两项更改。首先,在异构比较中,只将int值与b组件进行比较:

    bool operator()(const Example& left, int right) const
    {
        return left.b > right;
    }

(鉴于我们即将进行的第二次更改,您不需要镜像比较)。

其次,使用lower_bound 代替find:

auto it=sa.lower_bound(3);
if(it!=sa.end() && it->b == 3) { /* Found */ }

请注意,我们现在必须另外确认找到的元素具有正确的b 组件;例如sa.lower_bound(4) 也会找到 {5, 3} 元素。从积极的方面来说,lower_bound 保证给我们排序顺序中的第一个元素,一个最大的 a。

Demo

【讨论】:

  • 是的,正如我所写的。使用std::set 和自定义比较器,它会找到一些带有 3 的值。使用附加功能非常简单。但是我根本不需要任何比较器。 . .
  • @ArminMontigny 我不确定我明白你在说什么。我的解决方案确实也使用了比较器,并且也确实以对数时间找到了 b==3 和最大 a 的值。你说的这个“附加功能”是什么?
  • 我的意思是附加功能,例如std::lower_bound。我的函数也会找到 5,3。它很可能总是有效的。但它是依赖于实现的。 . .但不用担心。我发现您的解决方案非常好。你解释所有的背景和那个。所以,+1
  • 我的解决方案没有使用std::lower_bound
  • @IgorTandetnik 感谢您的回答。和我的要求差不多。但是,如果我需要在b 上进行排序时需要找到a 的值,是否有可能?我不需要最大的a,但我需要按照你上面提到的排序并找到a 的值,比如 3 或其他实际上是唯一的。
猜你喜欢
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多