与 stl sort() 的比较

发布于 2024-12-17 05:03:07 字数 559 浏览 0 评论 0原文

我正在尝试在类函数中使用 stl sort() 。我想对如下所示的结构数组进行排序:

struct foo{
    double num;
    std::string s;
};

使用如下比较函数:

bool aGreaterThanb(foo a, foo b){
    if (a.num > b.num){
        if(a.num == b.num){
            if (anotherOutsideComparison(a.s, b.s)){
                return true;
            }
        }
        else
            return true;
    }
    else
        return false;
}

但我不确定如何格式化它以使其进行编译。我应该如何格式化它以便我可以调用 sort(fooarray[0], fooarray[end], aGreaterThanb);? (举个例子就太好了)

I'm trying to use stl sort() in a class function. I would like to sort an array of structs that look like this:

struct foo{
    double num;
    std::string s;
};

with a comparison function like this:

bool aGreaterThanb(foo a, foo b){
    if (a.num > b.num){
        if(a.num == b.num){
            if (anotherOutsideComparison(a.s, b.s)){
                return true;
            }
        }
        else
            return true;
    }
    else
        return false;
}

But I'm not sure how I can format this to get it to compile. How should I format this so I can call sort(fooarray[0], fooarray[end], aGreaterThanb);? (An example would be great)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

初懵 2024-12-24 05:03:07

将比较函数编写为称为函子的结构的 operator() 方法:

struct aGreaterThanb
{
    bool operator() (const foo& a, const foo& b)
    {
        // return true iff a is strictly less than b according to your ordering
    }
};

然后将该函子对象的实例传递给 std::sort

std::sort(fooarray.begin(), fooarray.end(), aGreaterThanb());

Write your comparison function as the operator() method of a structure called a functor:

struct aGreaterThanb
{
    bool operator() (const foo& a, const foo& b)
    {
        // return true iff a is strictly less than b according to your ordering
    }
};

Then pass an instance of that functor object to std::sort:

std::sort(fooarray.begin(), fooarray.end(), aGreaterThanb());
无需解释 2024-12-24 05:03:07

如果您使用像这样的 foo 数组:

foo fooarray[Foos];
...
sort(fooarray, fooarray + Foos, &aGreaterThanb);

上面的代码将以相反的顺序对数组进行排序,因为 sort 需要一个小于比较器。

此外,为了避免为了比较而复制大量 foo 对象,请声明您的比较器以 const foo& 而不是 foo 作为参数。

bool aGreaterThanb(const foo& a, const foo& b) {

If you are using an array of foo like this:

foo fooarray[Foos];
...
sort(fooarray, fooarray + Foos, &aGreaterThanb);

The above code would sort your array in reverse order, since sort expects a less-than comparator.

Additionally to avoid copying a lot of foo-objects around just for comparison, declare your comparator to take const foo& instead of foo as arguments.

bool aGreaterThanb(const foo& a, const foo& b) {
诗化ㄋ丶相逢 2024-12-24 05:03:07

您应该将迭代器(指针的广义超集)传递给 STL sort 函数:

std::sort(fooarray, fooarray + end, &aGreaterThanb);

You're supposed to pass iterators — a generalized superset of pointers — to the STL sort function:

std::sort(fooarray, fooarray + end, &aGreaterThanb);
白色秋天 2024-12-24 05:03:07

它已经按照您想要的方式工作了:

#include <algorithm>
int main()
{
    foo     data[10];
    std::sort(&data[0], &data[10], aGreaterThanb);
}

但是您有语法错误。您缺少一个大括号:

        return true;
} // <--- Missing this line
else
    return false;

为了提高效率,您应该通过 const 引用传递:

bool aGreaterThanb(foo const& a, foo const& b){

It works just as you want already:

#include <algorithm>
int main()
{
    foo     data[10];
    std::sort(&data[0], &data[10], aGreaterThanb);
}

But you have syntax error. You are missing a brace:

        return true;
} // <--- Missing this line
else
    return false;

For efficiency you should pass by const reference:

bool aGreaterThanb(foo const& a, foo const& b){
熊抱啵儿 2024-12-24 05:03:07

请注意,在最坏的情况下,排序函数最多进行 N^2 次比较。
stable_sort复杂度在N*logN和N*(LogN^2)之间

Note that in worst case sort function is up to N^2 comparsions.
And stable_sort complexity is between N*logN and N*(LogN^2)

不弃不离 2024-12-24 05:03:07

让它成为一个运营商。

struct foo {
    double num;
    std::string s;
};

bool operator>(const foo& a, const foo& b) {
    return (
        (a.num > b.num) ||
        ((a.num == b.num) &&
        anotherOutsideComparison(a.s, b.s))
    );
}

// note: std::sort expects operator<
bool operator<(const foo& a, const foo& b) {
    return b > a;
}

如果您确实想使用 operator> 进行排序,请传递 std::greater() 作为函子。

std::sort(foos.begin(), foos.end(), std::greater<foo>());

Make it an operator.

struct foo {
    double num;
    std::string s;
};

bool operator>(const foo& a, const foo& b) {
    return (
        (a.num > b.num) ||
        ((a.num == b.num) &&
        anotherOutsideComparison(a.s, b.s))
    );
}

// note: std::sort expects operator<
bool operator<(const foo& a, const foo& b) {
    return b > a;
}

If you really want to sort using operator>, pass std::greater<foo>() as the functor.

std::sort(foos.begin(), foos.end(), std::greater<foo>());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文