C++20中的三路比较运算符_c++三路比较 - wpsshop博客
当前位置:   article > 正文

C++20中的三路比较运算符_c++三路比较

c++三路比较

一、operator<=>

三路比较运算符也是一个c++20新提供的一个功能,网上有的人叫“航天飞机运算符”也有的叫“宇宙飞船运算符”,知道啥意思就行。在以前的c++编程中,实现基本类型的比较功能是由标准语法直接提供的,但在实际的编程中,经常会遇到这样一些问题,封装好的对象(类和结构体对象)也会出现一些比较的要求,这时候儿,一般会通过重载某个运算符来实现特定的对象的比较。而在c++20中提供的这个三路比较运算符,会默认生成一系列的比较运算符,如果默认生成的语义不符合自己的标准,那么可以自定义其并由编译器重新生成符合预期的重载运算语义。生成的默认运算符有六个即:==,!=,<,>,<=,>=。基本的操作如下:

auto operator<=>(const Point&) const = default;

自定义时有三种可用的返回类型:
std::strong_ordering:强比较,严格按照比较的顺序、方式来进行,不能从下面两个转回,特别注意的是它不区分等价值。
std::weak_ordering:弱比较,对比较的大小写,可以对等价的字符串用某种方式区别
std::partial_ordering:偏序比较,其实就是自定义,把直观上不可能比较的对象通过某种方式来进行比较。
更详尽的细节可以看一下官网:

  1. https://zh.cppreference.com/w/cpp/utility
  2. https://zh.cppreference.com/w/cpp/language/default_comparisons

二、c++20中的用法

看一下官网修改的例子:

  1. #include <set>
  2. #include <iostream>
  3. class Point
  4. {
  5.     int x = 0;
  6.     int y = 0;
  7. public:
  8.     void SetData(int x, int y) { this->= x; this->= y; };
  9.     auto operator<=>(const Point&) const = default;
  10.     // ……非比较函数……
  11. };
  12. void TestThreeOprator()
  13. {
  14.     // 编译器生成全部四个关系运算符
  15.     Point pt1, pt2;
  16.     pt1.SetData(0,1);
  17.     std::set<Point> s; // OK
  18.     s.insert(pt1); // OK
  19.     if (pt1 <= pt2)
  20.     {
  21.         /*...*/
  22.         std::cout<< "is ok!" << std::endl;
  23.     }
  24.     else
  25.     {
  26.         std::cout << "is err!" << std::endl;
  27.     }
  28.     // OK,只调用一次 <=>
  29. }

再看一个自定义的例子:

  1. class Base {
  2. public:
  3.     auto operator<=>(const Base&) const = default;
  4. };
  5. std::strong_ordering operator <=>(const std::string& a, const std::string& b) {
  6.     int cmp = a.compare(b);
  7.     if (cmp < 0return std::strong_ordering::less;
  8.     else if (cmp > 0return std::strong_ordering::greater;
  9.     else return std::strong_ordering::equivalent;
  10. }
  11. class TotallyOrdered : Base {
  12.     std::string tax_id;
  13.     std::string first_name;
  14.     std::string last_name;
  15. public:
  16.     TotallyOrdered(const std::string& id, const std::string& first, const std::string& last)
  17.         :tax_id(id), first_name(first), last_name(last) {}
  18.     // 定制 operator<=>,因为我们想先比较姓
  19.     std::strong_ordering operator<=>(const TotallyOrdered& that) const {
  20.         if (auto cmp = (Base&)(*this) <=> (Base&)that; cmp != 0return cmp;
  21.         if (auto cmp = last_name <=> that.last_name; cmp != 0return cmp;
  22.         if (auto cmp = first_name <=> that.first_name; cmp != 0return cmp;
  23.         return tax_id <=> that.tax_id;
  24.     }
  25.     // ……非比较函数……
  26. };
  27. void test_compare02() {
  28.     // 编译器生成全部四个关系运算符
  29.     TotallyOrdered to1"8""marding""taid" }, to2"2""zhuli""lomb" };
  30.     std::set<TotallyOrdered> s; // ok
  31.     s.insert(to1); // ok
  32.     s.insert(to2);
  33.     if (to1 <= to2) {  
  34.         std::cout << "is ok"<<std::endl;
  35.     }
  36.     else {
  37.         std::cout << "is err!"<<std::endl;
  38.     }
  39. }

再看一个弱比较的例子:

  1. #include <iostream>
  2. #include <compare>
  3. struct Rational_2 {
  4.     int num;
  5.     int den; // > 0
  6. };
  7. constexpr std::weak_ordering operator<=>(Rational_2 lhs, Rational_2 rhs)
  8. {
  9.     return lhs.num * rhs.den <=> rhs.num * lhs.den;
  10. }
  11. void print(std::weak_ordering value)
  12. {
  13.     if (value == 0)
  14.         std::cout << "equal\n";
  15.     else if (value < 0)
  16.         std::cout << "less\n";
  17.     else
  18.         std::cout << "greater\n";
  19. }
  20. int main()
  21. {
  22.     Rational_2 c{6,5};
  23.     Rational_2 d{8,7};
  24.     print(c <=> d);
  25.     print(std::compare_three_way{}(c,d));
  26. }

这个资料在官网还是微软的网站还是在相关的网络上都有大量的资料,这里特别细节的就请大家去看上面的两个网站,内部的跳转会提供更多的细节的代码和说明。

三、总结

c++20表明c++的标准委员会的人确实没闲着,在进步,但网上对c++20的诟病也是不少,大老外们对它的效率提出了一个又一个质疑,有质疑是好事儿,互相促进,互相提高。如果没人质疑,反倒是说明c++要凉了。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/673025
推荐阅读
相关标签
  

闽ICP备14008679号