简体   繁体   English

关于std :: sort的比较函数对象

[英]About the comparison function object of std::sort

In the following two examples: example 1: 在下面的两个示例中:示例1:

struct less_than_key
{
    inline bool operator() (const MyStruct& struct1, const MyStruct& struct2)
    {
        return (struct1.key < struct2.key);
    }
};

std::vector < MyStruct > vec;

vec.push_back(MyStruct(4, "test"));
vec.push_back(MyStruct(3, "a"));

std::sort(vec.begin(), vec.end(), less_than_key());  //LINE1

Example 2: 范例2:

std::array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; 
struct {
    bool operator()(int a, int b)
    {   
        return a < b;
    }   
} customLess;
std::sort(s.begin(), s.end(), customLess);        //LINE2

Why the compare function object at LINE1 has "()", ie, less_than_key()? 为什么LINE1的比较函数对象具有“()”,即less_than_key()? The compare function object at LINE2 does not have "()", ie, customLess? LINE2的比较函数对象没有“()”,即customLess?

Line 1 has parentheses because it's making an instance of the less_than_key class and passes it as a parameter (ie it's making a value). 第1行带有括号,因为它正在创建less_than_key类的实例,并将其作为参数传递(即,它是一个值)。

Line 2 doesn't have parentheses because it already is an instance of the anonymous struct provided (ie it's already a value). 第2行没有括号,因为它已经是所提供的匿名结构的一个实例(即,它已经是一个值)。

std::sort expects an instance of the comparator object to work, which is why you can sometimes construct them in place or provide one that is already constructed. std::sort希望比较器对象的一个​​实例正常工作,因此您有时可以就地构造它们或提供一个已经构造的对象。

In the example 1 you are declaring a struct called less_than_key, but you don't have any instance of this struct, you have only a declaration. 在示例1中,您声明了一个名为less_than_key的结构,但是您没有该结构的任何实例,只有一个声明。 In LINE 1 you are calling the constructor of the struct and creating an instance of the object to be used by std::sort function. 在LINE 1中,您将调用struct的构造函数,并创建要由std :: sort函数使用的对象的实例。

In the example 2 it is different. 在示例2中,情况有所不同。 You are also declaring a struct, this time without name (anonymous), but the difference is that you are creating an instance "customLess" of this type (calling the constructor implicitly). 您还声明了一个结构,这次没有名称(匿名),但是区别在于您正在创建这种类型的实例“ customLess”(隐式调用构造函数)。 So, as you already have the instance created, you don't need to create it in the std::sort function. 因此,既然已经创建了实例,则无需在std :: sort函数中创建它。

Look at the difference in the declaration of the structs: 看一下结构声明中的区别:

1.- struct less_than_key { ... }; 1.-结构less_than_key {...};

2.- struct [some_name_if_wanted] { ... } customLess ; 2.-结构[some_name_if_wanted] {...} customLess ;

std::sort expects a comparison function object . std::sort需要一个比较函数对象 In the first example, you're constructing a object. 在第一个示例中,您正在构造一个对象。 In the second example, you already have one. 在第二个示例中,您已经有一个。 The easiest example would be to look at the transparent comparators, ie std::less<>() , which is a class template. 最简单的例子是查看透明的比较器,即std::less<>() ,它是一个类模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM