C ++ STL-STL sort()的第三个参数如何工作? [英] C++ STL - How does the third argument of the STL sort() work?

查看:173
本文介绍了C ++ STL-STL sort()的第三个参数如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望根据数据对象'age'对class Person的对象数组进行排序.我将对象存储在vector<Person> v.

I wish to sort an array of objects of class Person on the basis of its data member 'age'. I store the objects in a vector<Person> v.

据我所知,至少有4种方法可以执行此操作,并且基于以下编写的方法,我有以下问题.

As far I have understood, there can be at least 4 ways to perform this action and I have the following questions based on the methods written below.

  1. 在类内部定义的operator()如何工作?我是否不应该使'<'重载运营商在这里也是如此?为什么是'()'?

  1. How does operator() defined inside a class work? Should I not overload the '<' operator here as well? Why '()' ?

我在方法1中发送了一个对象作为第三个参数.但是,在方法2中,我发送了函数的名称.为什么会这样?

I sent an object as the 3rd parameter in method 1. But, in method 2, I sent the name of a function. Why is it like that?

这四种方法中哪一种是最好的?我觉得方法3最简单.

Which of the four methods is the best? I felt that method 3 was the easiest.

方法1

class cmp
{
public:
    bool operator() (  Person const &a,  Person const &b )
    {
        return a.age < b.age ;
    }
};

sort( v.begin(), v.end(), cmp());

方法2

bool cmp( const Person a, const Person b ) 
{
    return a.age < b.age ;
}

sort( v.begin(), v.end(), cmp );

方法3

bool operator < ( const Person a, const Person b )
{
    return a.age < b.age ;
}

sort( v.begin(), v.end());

方法4

//using lambda expression
sort( v.begin(), v.end(), [](const Person &a, const Person &b){return a.age < b.age;});

推荐答案

要使用std::sort(或与此相关的任何函数)对范围进行排序,它需要知道如何比较范围中的两个元素/em>,以确定小于(或大于)的关系.

To sort a range using std::sort (or any function for that matter), it needs to know how two elements from the range are compared, in order to determine less than (or greater than) relationship.

标准库函数std::sort两种风格:一种使用operator<,另一种使用 compare 函数/功能.您已经在代码中同时使用了这两种方法—特别是,示例中的第三个使用<,其余的使用 compare function/functor.

The Standard Library function std::sort comes in two flavors: one uses operator<, the other uses a compare function/functor. You've used both of them in your code — in particular, the third one in your example uses < and the rest use compare function/functor.

关于哪种方法最好?

嗯,这取决于.使用operator<的代码是较不灵活的,因为它是固定的,但也要求您键入更少.足够时使用它.

Well, it depends. The one which uses operator< is less flexible since it is fixed but requires you less typing as well. Use it when it suffices.

另一个更灵活,因为您可以传递任何比较函数并相应地对元素进行排序.当operator<不足够时使用它.另外,当您选择这种口味时,您还有其他选择:比较器可以是 function functor lambda &mdash ;如果使用函数或函子(在名称空间级别定义),则可以重用;另一方面,lambda通常是在函数范围内定义的,因此,它不是可重用的,除非您在名称空间范围内定义它,在这种情况下,它几乎是相同的.作为功​​能.

The other one is more flexible as you can pass any compare function and get your elements sorted accordingly. Use it when operator< doesn't suffice. Also, when you choose this flavor, you have other choices as well : the comparer could be a function, a functor, or a lambda — if you use function or functor (defined at namespace level), then you can reuse them; on the other hand, lambda is usually defined in a function scope, so it is not that reusable, unless you define it at namespace scope, in which case it is almost same as function.

示例,假设您要对int的向量进行升序排序:

Example, suppose you want to sort a vector of int in increasing order:

 std::vector<int>  v{10, 3, 12, -26};
 std::sort(v.begin(), v.end());
 print(v);

输出:-26,3,10,12.因此operator<确实可以完成这项工作.

Output: -26,3,10,12. So operator< does do the job.

但是,如果您希望仅考虑幅值(即忽略符号)对元素进行排序,那您就必须使用另一种风味:

But what if you want the elements sorted considering only magnitude (i.e ignore signs), then you have to use the other flavor:

 std::vector<int>  v{10, 3, 12, -26};
 auto abs_cmp = [](int a, int b) { return std::abs(a) < std::abs(b); };
 std::sort(v.begin(), v.end(), abs_cmp);
 print(v);

输出:3,10,12,-26.在这种情况下,这就是您期望的输出.

Output : 3,10,12,-26. That is the output you would expect in this case.

希望有帮助.

这篇关于C ++ STL-STL sort()的第三个参数如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆