运算符<比较多个字段 [英] operator< comparing multiple fields

查看:153
本文介绍了运算符<比较多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下运算符<应该先按一个值排序,然后再按另一个值排序:

I have the following operator< that is supposed to sort first by a value, then by another value:

    inline bool operator < (const obj& a, const obj& b) 
    {
        if(a.field1< b.field1)
            return true;
        else
            return a.field2 < b.field2;
    }

我感觉这是不正确的,如果没有对成员变量进行另一个第三次比较测试,您将无法做到这一点,但是我找不到任何无法解决此问题的示例. 那么,这真的能按预期进行排序吗? 谢谢

I have the feeling this is incorrect and that you can't do that without another third comparaison test on the members variables, but I can't find any example where this doesn't work. So whould this really sort as expected? thanks

我将其编码为:

    inline bool operator < (const obj& a, const obj& b) 
    {
        if(a.field1< b.field1)
            return true;
                    else if(a.field1> b.field1)
            return false;
        else
            return a.field2 < b.field2;
    }

有什么区别吗?我问是因为我知道我的经验是正确的,但比第一个要长

are there any differences? I'm asking because I know mine is correct from experience but also longer than the first one

推荐答案

我想一个人做..

仅当Obj::field1的值相等时,才应该比较Obj::field2的值.

I'd like to do it all by myself..

You should only compare the values of Obj::field2 if the values of Obj::field1 are equal.

/* This will meet the requirements of Strict-Weak-Ordering */

if (a.field1 != b.field1) return a.field1 < b.field1;
else                      return a.field2 < b.field2;

正确(推荐)方式:

"正确"的实现方式仅使用operator<来比较字段,因此下面看起来比实际情况要复杂得多.

The correct (recommended) way:

The "correct" way of implementing it uses only operator< to compare the fields, the below looks more complicated than it really is.

但是,它将产生与先前编写的易于理解示例相同的结果.

It will however yield the same result as the easy-to-understand example previously written.

return a.field1 < b.field1 || (
  !(b.field1 < a.field1) && a.field2 < b.field2
);


必须有一种实现operator<的方法而不会引起很多麻烦吗?

C ++ 11

您可以使用 STL 中的std::tuple,对于已经定义的多个字段,它已经具有operator<,例如下面的示例.


There must be a way of implementing operator< without causing a lot of headache?

C++11

You can use std::tuple from the STL which already have operator< for multiple fields defined, such as in the below example.

#include <utility>

...

inline bool
operator< (Obj const& lhs, Obj const& rhs)
{
  return std::tie (lhs.field1, lhs.field2) < std::tie (rhs.field1, rhs.field);
}

C ++ 03

如果您的编译器尚不支持C ++ 11,而您只需要比较每个对象中的两个字段,则可以使用std::pair.

std::make_pair的原因与上一个使用std::tie的示例相同.

The reason for std::make_pair is the same as in the previous example using std::tie.

#include <utility>

...

inline bool
operator< (Obj const& lhs, Obj const& rhs)
{
  return std::make_pair (lhs.field1, lhs.field2)
       < std::make_pair (rhs.field1, rhs.field2);
}

使用std::pair将需要创建成员的副本,这在某些情况下是不可取的.

using std::pair will require copies of the members to be created, which in some circumstances is undesirable.

有关更多信息,请参见以下问题/解答,但请进行总结; c ++ 11方法不会造成太大的开销,并且实现起来非常简单.

See the below question/answers for more information, but to sum it up; the c++11 approach doesn't cause that much overhead and it's very simple to implement.

  • 通过'tuple'实现比较运算符和领带",一个好主意?

这篇关于运算符&lt;比较多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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