C++20三向比较运算符详解
创始人
2024-11-14 22:33:48
0

三向比较运算符可以用于确定两个值的大小顺序,也被称为太空飞船操作符。使用单个表达式,它可以告诉一个值是否等于,小于或大于另一个值。

它返回的是类枚举(enumeration-like)类型,定义在 std 名称空间中。其运算的结果如下:

数据类型含义
如果操作数为整数类型,其结果称为 强排序(strong_ordering)strong_ordering::less左边的操作数小于右边的操作数
strong_ordering::greater左边的操作数大于右边的操作数
strong_ordering::equal左边的操作数等于右边的操作数
如果操作数为浮点类型,其结果称为 偏序(partial_ordering)partial_ordering::less左边的操作数小于右边的操作数
partial_ordering::greater左边的操作数大于右边的操作数
partial_ordering::equivalent左边的操作数等于右边的操作数
partial_ordering::unordered无法比较时(有一个非数字)
如果操作数为自己实现的类型,称为 弱排序(weak_ordering)weak_ordering::less左边的操作数小于右边的操作数
weak_ordering::greater左边的操作数大于右边的操作数
weak_ordering::equivalent左边的操作数等于右边的操作数

三向比较运算符的用法

三向比较运算符的使用方式如下:

#include   using namespace std;  int main() {     int i{ 11 };     strong_ordering result{ i <=> 0 };     cout << typeid(result).name() << endl;      if (result == strong_ordering::less) { cout << "less" << endl; };     if (result == strong_ordering::greater) { cout << "greater" << endl; };     if (result == strong_ordering::equal) { cout << "equal" << endl; };      return 0; } 

可以得到以下的输出结果

struct std::strong_ordering greater 

同时, 还提供了一些函数来解释排序结果,有以下几种:std::is_eq()is_neq()is_lt()is_lteq()is_gt()is_gteq()

使用的方式如下:

#include  #include  using namespace std;  int main() {     int i{ 11 };     strong_ordering result{ i <=> 0 };     cout << typeid(result).name() << endl;      if (is_lt(result)) { cout << "less" << endl; };     if (is_gt(result)) { cout << "greater" << endl; };     if (is_eq(result)) { cout << "equal" << endl; };      return 0; } 

三向比较运算符的作用

在编写自己的类时,如果需要实现比较运算符,在 C++20 之前,通常要重载 6 个比较运算符:(><<=>===, !=)。在 C++20 引入了三向比较运算符以后,就可以只重载 <=>== 后,其他的运算符会由编译器为我们生成。(在 C++20 中,重载了 == 后,!= 也会自动重载)

以下是使用示例:

#include  #include  using namespace std;  class MyClass { public:     MyClass(int ini) : _data{ ini } {}      auto operator<=>(const MyClass& other) const {         return this->_data <=> other._data;     }     auto operator==(const MyClass& other) const {         return this->_data == other._data;     } private:     int _data; };  int main() {     MyClass a{ 10 }, b{ 20 };      if (a < b) {         cout << "a < b" << endl;     }     if (a != b) {         cout << "a != b" << endl;     }      return 0; } 

可以得到以下的运算结果:

a < b a != b 

相关内容

热门资讯

第1分钟辅助器外挂!wepok... 第1分钟辅助器外挂!wepoker透视脚本苹果版,wpk透视是真的吗(有挂解密)-哔哩哔哩1、玩家可...
方法辅助挂!玉海楼茶苑app辅... 方法辅助挂!玉海楼茶苑app辅助!一贯真的有开挂辅助app(有挂教学)-哔哩哔哩1、玉海楼茶苑app...
三分钟辅助器外挂!wpk有透视... 三分钟辅助器外挂!wpk有透视挂吗,wepoker安装教程(有挂方略)-哔哩哔哩;1、完成wepok...
现场直击!指尖四川辅助脚本苹果... 现场直击!指尖四川辅助脚本苹果版!总是真的有开挂辅助插件(有挂方式)-哔哩哔哩1)指尖四川辅助脚本苹...
第八分钟辅助器外挂!wepok... 第八分钟辅助器外挂!wepoker透视方法,wepokerplus透视脚本免费(有挂头条)-哔哩哔哩...
围绕透视问题!微乐家乡麻辣自建... 围绕透视问题!微乐家乡麻辣自建房辅助app!其实存在有开挂辅助工具(有挂教学)-哔哩哔哩1、微乐家乡...
四分钟辅助器外挂!wepoke... 四分钟辅助器外挂!wepoker好友局透视,淘宝买wepoker透视有用吗(有挂教程)-哔哩哔哩1、...
记者获悉!花花生活圈私人局辅助... 记者获悉!花花生活圈私人局辅助器!真是真的是有开挂辅助脚本(有挂秘籍)-哔哩哔哩1、任何花花生活圈私...
第5分钟辅助器外挂!wpk透视... 第5分钟辅助器外挂!wpk透视插件,wepoker私人局有透视吗(有挂教学)-哔哩哔哩wepoker...
第四分钟辅助器外挂!德普之星透... 第四分钟辅助器外挂!德普之星透视辅助软件是真的吗,wepoker破解版内购(有挂规律)-哔哩哔哩1、...