C++20三向比较运算符详解

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

它返回的是类枚举(enumeration-like)类型,定义在 <compare>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 左边的操作数等于右边的操作数

三向比较运算符的用法

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

cpp 复制代码
#include <iostream>

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

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

使用的方式如下:

cpp 复制代码
#include <iostream>
#include <compare>
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 中,重载了 == 后,!= 也会自动重载)

以下是使用示例:

cpp 复制代码
#include <iostream>
#include <compare>
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
相关推荐
「QT(C++)开发工程师」18 小时前
C++17三大实用特性详解:内联变量、std::optional、std::variant
jvm·c++
不爱吃炸鸡柳18 小时前
C++ STL 核心:string 从入门到精通(面试+源码+OJ实战)
java·c++·面试
南境十里·墨染春水18 小时前
C++笔记 Lambda表达式
开发语言·c++·笔记
悟渔18 小时前
用于STM32的C++编程的LED对象
c++·stm32·单片机
17(无规则自律)19 小时前
DFS:带重复项的全排列,程序运行全流程解析
c++·算法·深度优先
郝学胜-神的一滴19 小时前
「栈与缩点的艺术」二叉树前序序列化合法性判定:从脑筋急转弯到工程实现
java·开发语言·数据结构·c++·python·算法
AIminminHu19 小时前
OpenGL渲染与几何内核那点事-项目实践理论补充(三-1-(3):番外篇-当你的CAD打开“怪兽级”STL时:从内存爆炸到零拷贝的极致优化
c++·零拷贝·mmap·内存拷贝
水饺编程19 小时前
第4章,[标签 Win32] :SysMets3 程序讲解04,垂直滚屏重绘
c语言·c++·windows·visual studio
xiaoye-duck19 小时前
《算法题讲解指南:动态规划算法--子序列问题(附总结)》--32.最长的斐波那契子序列的长度,33.最长等差数列,34.等差数列划分II-子序列
c++·算法·动态规划
BestOrNothing_201519 小时前
C++零基础到工程实战(1.3):cpp注释与输出详解
c++·注释·命名空间·初学者教程·cout输出