C ++比较运算符 [英] C++ comparison operators

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

问题描述

我经常看到人们只覆盖运算符< ;,而不是>或==.这是否意味着默认情况下,operator>和operator ==是使用operator< ?

I frequently see people only overriding operator<, not > or ==. Does it mean that by default, operator> and operator== are implemented using operator< ?

我还经常看到人们在写作(请参阅这里)

I also frequently see people writing (see here)

bool operator() (Node const& n1, Node const& n2) const
{
    // TODO: your condition
    return n1.a < n2.a;
}

那么operator()在这里是什么意思?似乎非常违反直觉.

What does operator() mean here then? it seems very counter-intuitive.

推荐答案

它们仅覆盖<的原因是因为默认情况下,这是有序容器用来比较值的原因,因此它们仅需定义即可回答问题.

The reason they're only overriding < is because by default that's what ordered containers use to compare values, so that's all they need to define to answer the question.

#include <set>

struct my_fancy_integer
{
    int fancy;
};

// This is all std::set (or any ordered container) needs by default,
// So for an example answer I won't do anything else (as I expect you to
// learn and understand *why* it needs this by default).
bool operator<(const my_fancy_integer& first, const my_fancy_integer& second)
{
    return first.fancy < second.fancy;
}

// But I should really also defined the other comparison operators...
// For example, without operator> defined this would fail:
//
// std::set<my_fancy_integer, std::greater<my_fancy_integer>> x;
//
// But since you read documentation for std::set and std::greater you
// understand why this fails: std::set will use std::greater to order
// the values, and std::greater (by default) will try to use operator>.

int main()
{
    std::set<my_fancy_integer> x; // okay
}

否,其他运算符没有隐式定义(也不包含其他任何定义).在实际的应用程序中,如果已定义一个,则应全部定义它们.

No, the other operators are not implicitly defined in terms of it (nor in terms of anything else). In a real application, if you've defined one you should define them all.

或者,如果<在语法上对您的类型没有意义,但是对它们进行排序仍然很有价值,请定义一个可用的默认谓词,用户应将其传递给有序容器的谓词模板参数.

Alternatively, if < doesn't make sense for your type syntactically, but ordering them is still valuable, define a usable default predicate that users should pass to the ordered container's predicate template argument.

#include <set>
#include <string>
#include <tuple>

struct my_employee
{
    std::string name;
    int salary;
    int yearsEmployed;
};

// Saying one employee is "less" than another doesn't really make sense...
// But I can still give an *ordering* on them:
struct my_employee_ordering
{
    bool operator()(const my_employee& first, const my_employee& second) const
    {
        // I'll just reuse std::tuple's comparison operator, and tie the
        // fields of each structure into a tuple to use it. This orders
        // by name, salary, then yearsEmployed.
        return std::tie(first.name, first.salary, first.yearsEmployed) <
               std::tie(second.name, second.salary, second.yearsEmployed);
    }
};

int main()
{
    // We need to tell std::set how to order employees:
    std::set<my_employee, my_employee_ordering> x; // okay
}


operator()函数调用运算符.它允许您的对象被调用":


operator() is the function call operator. It allows your object to be "called":

struct foo
{
    void operator()(int x) { std::cout << x << std::endl; }
};

foo f;
f(5); // calls foo::operator()(5)

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

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