首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::rel_ops::operators

Defined in header <utility>

?

?

template< class T > bool operator!=( const T& lhs, const T& rhs );

(1)

?

template< class T > bool operator>( const T& lhs, const T& rhs );

(2)

?

template< class T > bool operator<=( const T& lhs, const T& rhs );

(3)

?

template< class T > bool operator>=( const T& lhs, const T& rhs );

(4)

?

给定用户定义的operator==operator<类型对象T,实现其他比较运算符的常用语义。

1%29件工具operator!=就...而言operator==...

2%29件工具operator>就...而言operator<...

3%29件工具operator<=就...而言operator<...

4%29件工具operator>=就...而言operator<...

参数

lhs

-

left-hand argument

rhs

-

right-hand argument

返回值

1%29true如果lhs不平等rhs...

2%29true如果lhs更大rhs...

3%29true如果lhs少于或相等rhs...

4%29true如果lhs更大或更平等rhs...

可能的实施

第一版

*。

命名空间rel[医]OPS{Template<class T>bool运算符%21=%28 const T&lhs,const T&rhs%29{返回%21%28 lhs==rhs%29;}

第二版

命名空间rel[医]OPS{模板<类T>bool运算符>%28 Const T&LHS,Const T&RHS%29{返回RHS<LHS;}

第三版

命名空间rel[医]OPS{Template<class T>bool运算符<=%28 const T&lhs,const T&rhs%29{返回%21%28 rhs<lhs%29;}}

第四版

命名空间rel[医]OPS{Template<class T>bool操作符>=%28 const T&lhs,const T&rhs%29{返回%21%28 lhs<rhs%29;}

注记

加油。操作者提供了一种更通用的替代方案std::rel_ops...

二次

代码语言:javascript
复制
#include <iostream>
#include <utility>
 
struct Foo {
    int n;
};
 
bool operator==(const Foo& lhs, const Foo& rhs)
{
    return lhs.n == rhs.n;
}
 
bool operator<(const Foo& lhs, const Foo& rhs)
{
    return lhs.n < rhs.n;
}
 
int main()
{
    Foo f1 = {1};
    Foo f2 = {2};
    using namespace std::rel_ops;
 
    std::cout << std::boolalpha;
    std::cout << "not equal?     : " << (f1 != f2) << '\n';
    std::cout << "greater?       : " << (f1 > f2) << '\n';
    std::cout << "less equal?    : " << (f1 <= f2) << '\n';
    std::cout << "greater equal? : " << (f1 >= f2) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
not equal?     : true
greater?       : false
less equal?    : true
greater equal? : false

二次

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com