6

とから演算子>>=<=、およびを取得するにはどうすればよいですか?!===<

標準ヘッダー<utility>は、上記の演算子を演算子==andに関して定義する名前空間 std::rel_ops を定義します<が、それを使用する方法がわかりません (次のような定義を使用するようにコードを誘導します:

std::sort(v.begin(), v.end(), std::greater<MyType>); 

非メンバー演算子を定義した場所:

bool operator < (const MyType & lhs, const MyType & rhs);
bool operator == (const MyType & lhs, const MyType & rhs);

#include <utility>が指定した場合using namespace std::rel_ops;、コンパイラはまだそれを訴えbinary '>' : no operator found which takes a left-hand operand of type 'MyType'ます..

4

3 に答える 3

8

私は<boost/operators.hpp>ヘッダーを使用します:

#include <boost/operators.hpp>

struct S : private boost::totally_ordered<S>
{
  bool operator<(const S&) const { return false; }
  bool operator==(const S&) const { return true; }
};

int main () {
  S s;
  s < s;
  s > s;
  s <= s;
  s >= s;
  s == s;
  s != s;
}

または、非メンバー オペレーターを使用する場合は、次のようにします。

#include <boost/operators.hpp>

struct S : private boost::totally_ordered<S>
{
};

bool operator<(const S&, const S&) { return false; }
bool operator==(const S&, const S&) { return true; }

int main () {
  S s;
  s < s;
  s > s;
  s <= s;
  s >= s;
  s == s;
  s != s;
}
于 2013-02-07T17:04:17.027 に答える
1

実際にはそれだけ<で十分です。次のようにします。

a == b<=>!(a<b) && !(b<a)

a > b <=>b < a

a <= b<=>!(b<a)

a != b<=>(a<b) || (b < a)

対称の場合も同様です。

于 2013-02-07T16:54:42.947 に答える
0

> equals !(<=)
>= equals !(<)
<= equals == or <
!= equals !(==)

このようなもの?

于 2013-02-07T16:56:21.320 に答える