1

> や == ではなく、operator< のみをオーバーライドしている人をよく見かけます。デフォルトでは、 operator> と operator== が operator< を使用して実装されているということですか?

書いている人もよく見かけます(こちらをご覧ください

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

ここで operator() はどういう意味ですか? それは非常に直感に反するようです。

4

5 に答える 5

4

オーバーライドするだけの理由<は、順序付けられたコンテナーが値を比較するためにデフォルトで使用するものであるため、質問に答えるために定義する必要があるのはそれだけだからです。

#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
}

いいえ、他の演算子はそれに関して暗黙的に定義されていません (または他のものに関しても)。実際のアプリケーションでは、1 つを定義したら、それらすべてを定義する必要があります。

または、<構文的に意味をなさないが順序付けが重要な場合は、ユーザーが順序付けられたコンテナの述語テンプレート引数に渡す必要がある使用可能なデフォルト述語を定義します。

#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()関数呼び出し演算子です。オブジェクトを「呼び出す」ことができます。

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

foo f;
f(5); // calls foo::operator()(5)
于 2013-06-26T23:53:19.617 に答える
2

まず、いいえ。< の実装は、== および > を暗黙的に定義しません。標準ライブラリでは、リストの並べ替えや同様のタスクに比較に特化した比較演算子として < を使用するため、人々は < を定義する傾向があります。

operator() は関数呼び出し演算子と呼ばれます。基本的に、次のような struct foo があるとしましょう

struct foo {
    int operator()(int a, int b) {
        return a+b;
    }
};

fooここで、 calledのインスタンスがあれば、関数呼び出し演算子をx使用して、指定した 2 つのパラメーター (この場合は 6 と 5) を使用して呼び出します。x(6, 5)関数呼び出し演算子は、構造体を関数のように扱うためのものであり、任意の数と型のパラメーターを受け取ることも、パラメーターをまったくとらないこともできます。あなたが示した例では、その関数を含むオブジェクトが関数呼び出しとして使用されると、2 つのノード オブジェクトを比較trueし、< 演算子に従って最初のオブジェクトが 2 番目のオブジェクトよりも小さい場合に戻ります。

于 2013-06-26T23:56:14.887 に答える
1

定義されている最小限の比較演算子または順序付け演算子は<および==です。その他の比較演算子は、次のように定義できます。

operator != -- !operator==
operator >= -- !operator<
operator <= -- operator== || operator <
operator >  -- !(operator== || operator <)

ライブラリには、他のboostすべてのオペレータを生成するテンプレートが含まれています。例については、「less_than_comparable」を参照してください。

編集1:ソート機能でよく使用される順序付け操作を定義しています
operator()たとえば、ある関数を昇順用に定義し、別の関数を降順用に定義することができます。並べ替えるには、昇順の関数オブジェクトまたは降順の関数オブジェクトを渡します。

于 2013-06-26T23:54:04.720 に答える