命名空间
变体
操作

标准库头文件 <compare> (C++20)

来自 cppreference.com
< cpp‎ | header
 
 
标准库头文件
语言支持
<source_location> (C++20)
<exception>
<initializer_list> (C++11)
<compare> (C++20)

概念
<concepts> (C++20)
诊断
<system_error> (C++11)

内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用实用程序
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<debugging> (C++26)
<expected> (C++23)
<bitset>
<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)
<inplace_vector> (C++26)
迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<cmath>
<linalg> (C++26)
<numbers> (C++20)

时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17/26*)
<text_encoding> (C++26)
输入/输出
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/26*)
正则表达式
<regex> (C++11)
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<rcu> (C++26)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)

<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)

<barrier> (C++20)
<future> (C++11)
<hazard_pointer> (C++26)

C 兼容性
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (直到 C++20)

 

此头文件是 语言支持 库的一部分。

内容

概念

指定操作符 <=> 在给定类型上产生一致的结果
(概念) [编辑]

三向比较的结果类型,支持所有 6 个操作符,不可替换,并允许不可比较的值
(类) [编辑]
三向比较的结果类型,支持所有 6 个操作符,不可替换
(类) [编辑]
三向比较的结果类型,支持所有 6 个操作符,可替换
(类) [编辑]
所有给定类型可以转换为的最强的比较类别
(类模板) [编辑]
获取给定类型上三向比较操作符 <=> 的结果类型
(类模板) [编辑]
实现 x <=> y 的约束函数对象
(类) [编辑]

定制点对象

执行三向比较,并产生类型为 std::strong_ordering 的结果
(定制点对象)[编辑]
执行三向比较,并产生类型为 std::weak_ordering 的结果
(定制点对象)[编辑]
执行三向比较,并产生类型为 std::partial_ordering 的结果
(定制点对象)[编辑]
执行三向比较,并产生类型为 std::strong_ordering 的结果,即使 operator<=> 不可用
(定制点对象)[编辑]
执行三向比较,并产生类型为 std::weak_ordering 的结果,即使 operator<=> 不可用
(定制点对象)[编辑]
执行三向比较,并产生类型为 std::partial_ordering 的结果,即使 operator<=> 不可用
(定制点对象)[编辑]

函数

命名比较函数
(函数) [编辑]

[编辑] 概要

namespace std {
  // comparison category types
  class partial_ordering;
  class weak_ordering;
  class strong_ordering;
 
  // named comparison functions
  constexpr bool is_eq  (partial_ordering cmp) noexcept { return cmp == 0; }
  constexpr bool is_neq (partial_ordering cmp) noexcept { return cmp != 0; }
  constexpr bool is_lt  (partial_ordering cmp) noexcept { return cmp < 0; }
  constexpr bool is_lteq(partial_ordering cmp) noexcept { return cmp <= 0; }
  constexpr bool is_gt  (partial_ordering cmp) noexcept { return cmp > 0; }
  constexpr bool is_gteq(partial_ordering cmp) noexcept { return cmp >= 0; }
 
  // common comparison category type
  template<class... Ts>
  struct common_comparison_category {
    using type = /* see description */;
  };
  template<class... Ts>
    using common_comparison_category_t = typename common_comparison_category<Ts...>::type;
 
  // concept three_way_comparable
  template<class T, class Cat = partial_ordering>
    concept three_way_comparable = /* see description */;
  template<class T, class U, class Cat = partial_ordering>
    concept three_way_comparable_with = /* see description */;
 
  // result of three-way comparison
  template<class T, class U = T> struct compare_three_way_result;
 
  template<class T, class U = T>
    using compare_three_way_result_t = typename compare_three_way_result<T, U>::type;
 
  // class compare_three_way
  struct compare_three_way;
 
  // comparison algorithms
  inline namespace /* unspecified */ {
    inline constexpr /* unspecified */ strong_order = /* unspecified */;
    inline constexpr /* unspecified */ weak_order = /* unspecified */;
    inline constexpr /* unspecified */ partial_order = /* unspecified */;
    inline constexpr /* unspecified */ compare_strong_order_fallback = /* unspecified */;
    inline constexpr /* unspecified */ compare_weak_order_fallback = /* unspecified */;
    inline constexpr /* unspecified */ compare_partial_order_fallback = /* unspecified */;
  }
}

[编辑] 概念 three_way_comparable

namespace std {
  template<class T, class Cat>
    concept __ComparesAs =                // exposition only
      same_as<common_comparison_category_t<T, Cat>, Cat>;
 
  template<class T, class U>
    concept __PartiallyOrderedWith =      // exposition only
      requires(const remove_reference_t<T>& t, const remove_reference_t<U>& u) {
        { t <  u } -> boolean-testable;
        { t >  u } -> boolean-testable;
        { t <= u } -> boolean-testable;
        { t >= u } -> boolean-testable;
        { u <  t } -> boolean-testable;
        { u >  t } -> boolean-testable;
        { u <= t } -> boolean-testable;
        { u >= t } -> boolean-testable;
      };
 
  template<class T, class Cat = partial_ordering>
    concept three_way_comparable =
      __WeaklyEqualityComparableWith<T, T> &&
      __PartiallyOrderedWith<T, T> &&
      requires(const remove_reference_t<T>& a, const remove_reference_t<T>& b) {
        { a <=> b } -> __ComparesAs<Cat>;
      };
}

[编辑] 概念 three_way_comparable_with

namespace std {
  template<class T, class U, class Cat = partial_ordering>
    concept three_way_comparable_with =
      __WeaklyEqualityComparableWith<T, U> &&
      __PartiallyOrderedWith<T, U> &&
      three_way_comparable<T, Cat> &&
      three_way_comparable<U, Cat> &&
      common_reference_with<const remove_reference_t<T>&, const remove_reference_t<U>&> &&
      three_way_comparable<
        common_reference_t<
          const remove_reference_t<T>&, const remove_reference_t<U>&>, Cat> &&
      requires(const remove_reference_t<T>& t, const remove_reference_t<U>& u) {
        { t <=> u } -> __ComparesAs<Cat>;
        { u <=> t } -> __ComparesAs<Cat>;
      };
}

[编辑] std::partial_ordering

namespace std {
  class partial_ordering {
    int value;          // exposition only
    bool is_ordered;    // exposition only
 
    // exposition-only constructors
    constexpr explicit
      partial_ordering(eq v) noexcept :
        value(int(v)), is_ordered(true) {}      // exposition only
    constexpr explicit
      partial_ordering(ord v) noexcept :
        value(int(v)), is_ordered(true) {}     // exposition only
    constexpr explicit
      partial_ordering(ncmp v) noexcept :
        value(int(v)), is_ordered(false) {}   // exposition only
 
  public:
    // valid values
    static const partial_ordering less;
    static const partial_ordering equivalent;
    static const partial_ordering greater;
    static const partial_ordering unordered;
 
    // comparisons
    friend constexpr bool operator==(partial_ordering v, /* unspecified */) noexcept;
    friend constexpr bool
      operator==(partial_ordering v, partial_ordering w) noexcept = default;
    friend constexpr bool operator< (partial_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator> (partial_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator<=(partial_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator>=(partial_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator< (/* unspecified */, partial_ordering v) noexcept;
    friend constexpr bool operator> (/* unspecified */, partial_ordering v) noexcept;
    friend constexpr bool operator<=(/* unspecified */, partial_ordering v) noexcept;
    friend constexpr bool operator>=(/* unspecified */, partial_ordering v) noexcept;
    friend constexpr partial_ordering
      operator<=>(partial_ordering v, /* unspecified */) noexcept;
    friend constexpr partial_ordering
      operator<=>(/* unspecified */, partial_ordering v) noexcept;
  };
 
  // valid values' definitions
  inline constexpr partial_ordering partial_ordering::less(ord::less);
  inline constexpr partial_ordering partial_ordering::equivalent(eq::equivalent);
  inline constexpr partial_ordering partial_ordering::greater(ord::greater);
  inline constexpr partial_ordering partial_ordering::unordered(ncmp::unordered);
}

[编辑] std::weak_ordering

namespace std {
  class weak_ordering {
    int value;  // exposition only
 
    // exposition-only constructors
    constexpr explicit weak_ordering(eq v) noexcept : value(int(v)) {}  // exposition only
    constexpr explicit weak_ordering(ord v) noexcept : value(int(v)) {} // exposition only
 
  public:
    // valid values
    static const weak_ordering less;
    static const weak_ordering equivalent;
    static const weak_ordering greater;
 
    // conversions
    constexpr operator partial_ordering() const noexcept;
 
    // comparisons
    friend constexpr bool operator==(weak_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator==(weak_ordering v, weak_ordering w) noexcept = default;
    friend constexpr bool operator< (weak_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator> (weak_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator<=(weak_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator>=(weak_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator< (/* unspecified */, weak_ordering v) noexcept;
    friend constexpr bool operator> (/* unspecified */, weak_ordering v) noexcept;
    friend constexpr bool operator<=(/* unspecified */, weak_ordering v) noexcept;
    friend constexpr bool operator>=(/* unspecified */, weak_ordering v) noexcept;
    friend constexpr weak_ordering
      operator<=>(weak_ordering v, /* unspecified */) noexcept;
    friend constexpr weak_ordering
      operator<=>(/* unspecified */, weak_ordering v) noexcept;
  };
 
  // valid values' definitions
  inline constexpr weak_ordering weak_ordering::less(ord::less);
  inline constexpr weak_ordering weak_ordering::equivalent(eq::equivalent);
  inline constexpr weak_ordering weak_ordering::greater(ord::greater);
}

[编辑] std::strong_ordering

namespace std {
  class strong_ordering {
    int value;  // exposition only
 
    // exposition-only constructors
    constexpr explicit strong_ordering(eq v) noexcept :
      value(int(v)) {}    // exposition only
    constexpr explicit strong_ordering(ord v) noexcept :
      value(int(v)) {}   // exposition only
 
  public:
    // valid values
    static const strong_ordering less;
    static const strong_ordering equal;
    static const strong_ordering equivalent;
    static const strong_ordering greater;
 
    // conversions
    constexpr operator partial_ordering() const noexcept;
    constexpr operator weak_ordering() const noexcept;
 
    // comparisons
    friend constexpr bool operator==(strong_ordering v, /* unspecified */) noexcept;
    friend constexpr bool
      operator==(strong_ordering v, strong_ordering w) noexcept = default;
    friend constexpr bool operator< (strong_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator> (strong_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator<=(strong_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator>=(strong_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator< (/* unspecified */, strong_ordering v) noexcept;
    friend constexpr bool operator> (/* unspecified */, strong_ordering v) noexcept;
    friend constexpr bool operator<=(/* unspecified */, strong_ordering v) noexcept;
    friend constexpr bool operator>=(/* unspecified */, strong_ordering v) noexcept;
    friend constexpr strong_ordering
      operator<=>(strong_ordering v, /* unspecified */) noexcept;
    friend constexpr strong_ordering
    operator<=>(/* unspecified */, strong_ordering v) noexcept;
  };
 
  // valid values' definitions
  inline constexpr strong_ordering strong_ordering::less(ord::less);
  inline constexpr strong_ordering strong_ordering::equal(eq::equal);
  inline constexpr strong_ordering strong_ordering::equivalent(eq::equivalent);
  inline constexpr strong_ordering strong_ordering::greater(ord::greater);
}

[编辑] std::compare_three_way

namespace std {
  struct compare_three_way {
    template<class T, class U>
    constexpr auto operator()(T&& t, U&& u) const;
 
    using is_transparent = /* unspecified */;
  };
}

[编辑] 另请参阅

三向比较操作符 表达式 lhs <=> rhs (C++20)