简体   繁体   English

比较函数放在哪里(例如std :: sort)?

[英]Where to put comparison function for use with (e.g.) std::sort?

If I had a class Cell , for example, which I want to sort according to the following function ( x and y here being int member variables with accessors): 例如,如果我有一个Cell类,我想根据以下函数进行排序(此处的xy是具有访问器的int成员变量):

bool sortByCoordinates(const Cell& c1, const Cell& c2) {
    return c1.getX() < c2.getX() || (c1.getX() == c2.getX() && c1.getY() < c2.getY());
}

Where exactly is the best place to put this function so that I can use it with a functions such as std::sort ? 放置此函数的最佳位置到底在哪里,这样我就可以将其与std::sort等函数一起使用?

In the examples they just have the method floating in the source file above where it is needed, but in practice I want to keep it associated with the Cell class. 在示例中,它们只是将方法浮动在需要它的源文件中,但是在实践中,我想使其与Cell类保持关联。 I know that I could override the operator< but there might be other sort methods by which I'd like to sort Cell , and I'm not a big fan of overriding operators for code clarity's sake anyhow. 我知道我可以重写operator<但是可能还有其他排序方法,我想通过这些方法对Cell进行排序,并且出于代码清晰的考虑,我也不是重写运算符的忠实拥护者。

At the moment I have it as a static method in my Cell.h file, so that I can call it in when sorting like so: 目前,我在Cell.h文件Cell.h其作为static方法使用,以便在排序时可以这样调用它:

std::sort(cells.begin(), cells.end(), Cell::sortByCoordinates);

Is this the best practice for multiple (or even singular) custom sort functions, and is the header file the right place for them? 这是针对多个(甚至是单个)自定义排序函数的最佳实践,并且头文件是否适合它们? If not, what is? 如果没有,那是什么?

Doing it the way you describe is reasonable. 按照您的描述进行操作是合理的。 And defining the comparison function inline in the header itself is a good idea if you care about performance (rather than defining it in the .cpp file). 如果您关心性能(而不是在.cpp文件中定义),则在标头本身中inline定义比较函数是一个好主意。

Personally I have a different preference than you. 就我个人而言,我的偏好与您不同。 I would declare this reasonable default comparison function at namespace scope (ie right below the class), because as written it does not need privileged access to class members. 我将在命名空间范围(即,类的正下方)声明此合理的默认比较函数,因为按照书面要求,它不需要对类成员的特权访问。 And I would declare it as operator < . 我将其声明为operator < I don't think there is anything to be ashamed about in terms of making one function "special" when it seems to be a reasonable default ordering. 我认为在似乎是合理的默认顺序的情况下,使一个功能成为“特殊”功能并没有什么可耻的。

暂无
暂无

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

相关问题 排序谓词的链接(例如std :: sort) - Chaining of ordering predicates (e.g. for std::sort) 标准中的哪里是模板参数的语法,例如,`std::function<int(char)> `定义?</int(char)> - Where in the standard is the grammar for the template argument in, e.g., `std::function<int(char)>` defined? 为什么成员函数需要'&'(例如在std :: bind中)? - Why does a member function needs '&' (e.g. in std::bind)? 一系列相关函数的通用函数(例如std :: stoi,std :: stof,std :: stod等) - Generic function for a family of related functions (e.g. std::stoi, std::stof, std::stod etc) 隐式函数到函数指针转换不适用于标准容器(例如向量) - Implicit function-to-function-pointer conversion not working for std containers (e.g. vector) 无法将std :: sort中的cdef函数用作比较函数 - Unable to use cdef function in std::sort as comparison function 从函数返回const char *的正确方法,例如,重写std :: exception :: what() - Proper method of returning const char* from a function, e.g., overriding std::exception::what() 使用enable_if和SFINAE时函数参数类型推导(std容器,例如vector)失败 - function argument type deduction (std container e.g. vector) fails when using enable_if and SFINAE 为什么不能使用auto作为模板类型参数(例如std :: array <auto, 5> )? - Why can't you use auto as a template type argument (e.g. std::array<auto, 5>)? 扩展可变参数模板模板参数以用于例如 std::variant<T...> - Expand variadic template template parameters for use in e.g. std::variant<T...>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM