标准库头文件 <compare>
来自cppreference.com
此头文件是通用工具库的一部分。
目录 |
[编辑] 类
| (C++20) |
三路比较的结果类型,仅支持相等/不相等,且不可替换 (类) |
| (C++20) |
三路比较的结果类型,仅支持相等/不相等,且可替换 (类) |
| (C++20) |
三路比较的结果类型,支持所有 6 种运算符,不可替换,并允许不可比较的值 (类) |
| (C++20) |
三路比较的结果类型,支持所有 6 种运算符且不可替换 (类) |
| (C++20) |
三路比较的结果类型,支持所有 6 种运算符且可替换 (类) |
| (C++20) |
给定的全部类型都能转换到的最强比较类别 (类模板) |
[编辑] 函数
| 具名比较函数 (函数) | |
| (C++20) |
进行三路比较并产生 std::strong_ordering 类型结果 (函数模板) |
| (C++20) |
进行三路比较并产生 std::weak_ordering 类型结果 (函数模板) |
| (C++20) |
进行三路比较并产生 std::partial_ordering 类型结果 (函数模板) |
| (C++20) |
进行三路比较并产生 std::strong_equality 类型结果 (函数模板) |
| (C++20) |
进行三路比较并产生 std::weak_equality 类型结果 (函数模板) |
[编辑] 概要
namespace std { // 比较类别类型 class weak_equality; class strong_equality; class partial_ordering; class weak_ordering; class strong_ordering; // 具名比较函数 constexpr bool is_eq (weak_equality cmp) noexcept { return cmp == 0; } constexpr bool is_neq (weak_equality 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; } // 常用比较类别类型 template<class... Ts> struct common_comparison_category { using type = /* 见定义 */ ; }; template<class... Ts> using common_comparison_category_t = typename common_comparison_category<Ts...>::type; // 比较算法 template<class T> constexpr strong_ordering strong_order(const T& a, const T& b); template<class T> constexpr weak_ordering weak_order(const T& a, const T& b); template<class T> constexpr partial_ordering partial_order(const T& a, const T& b); template<class T> constexpr strong_equality strong_equal(const T& a, const T& b); template<class T> constexpr weak_equality weak_equal(const T& a, const T& b); }
[编辑] 类 std::weak_equality
namespace std { class weak_equality { int value; // 仅用于阐释 // 仅用于阐释的构造函数 explicit constexpr weak_equality(eq v) noexcept : value(int(v)) {} // 仅用于阐释 public: // 合法值 static const weak_equality equivalent; static const weak_equality nonequivalent; // 比较 friend constexpr bool operator==(weak_equality v, /* 未指明 */ ) noexcept; friend constexpr bool operator!=(weak_equality v, /* 未指明 */ ) noexcept; friend constexpr bool operator==(/* 未指明 */ , weak_equality v) noexcept; friend constexpr bool operator!=(/* 未指明 */ , weak_equality v) noexcept; }; // 合法值的定义 inline constexpr weak_equality weak_equality::equivalent(eq::equivalent); inline constexpr weak_equality weak_equality::nonequivalent(eq::nonequivalent); }
[编辑] 类 std::strong_equality
namespace std { class strong_equality { int value; // 仅用于阐释 // 仅用于阐释的构造函数 explicit constexpr strong_equality(eq v) noexcept : value(int(v)) {} // 仅用于阐释 public: // 合法值 static const strong_equality equal; static const strong_equality nonequal; static const strong_equality equivalent; static const strong_equality nonequivalent; // 转换 constexpr operator weak_equality() const noexcept; // 比较 friend constexpr bool operator==(strong_equality v, /* 未指明 */ ) noexcept; friend constexpr bool operator!=(strong_equality v, /* 未指明 */ ) noexcept; friend constexpr bool operator==(/* 未指明 */ , strong_equality v) noexcept; friend constexpr bool operator!=(/* 未指明 */ , strong_equality v) noexcept; }; // 合法值的定义 inline constexpr strong_equality strong_equality::equal(eq::equal); inline constexpr strong_equality strong_equality::nonequal(eq::nonequal); inline constexpr strong_equality strong_equality::equivalent(eq::equivalent); inline constexpr strong_equality strong_equality::nonequivalent(eq::nonequivalent); }
[编辑] 类 std::partial_ordering
namespace std { class partial_ordering { int value; // 仅用于阐释 bool is_ordered; // 仅用于阐释 // 仅用于阐释的构造函数 explicit constexpr partial_ordering(eq v) noexcept : value(int(v)), is_ordered(true) {} // 仅用于阐释 explicit constexpr partial_ordering(ord v) noexcept : value(int(v)), is_ordered(true) {} // 仅用于阐释 explicit constexpr partial_ordering(ncmp v) noexcept : value(int(v)), is_ordered(false) {} // 仅用于阐释 public: // 合法值 static const partial_ordering less; static const partial_ordering equivalent; static const partial_ordering greater; static const partial_ordering unordered; // 转换 constexpr operator weak_equality() const noexcept; // 比较 friend constexpr bool operator==(partial_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator!=(partial_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator< (partial_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator<=(partial_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator> (partial_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator>=(partial_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator==(/* 未指明 */ , partial_ordering v) noexcept; friend constexpr bool operator!=(/* 未指明 */ , partial_ordering v) noexcept; friend constexpr bool operator< (/* 未指明 */ , partial_ordering v) noexcept; friend constexpr bool operator<=(/* 未指明 */ , partial_ordering v) noexcept; friend constexpr bool operator> (/* 未指明 */ , partial_ordering v) noexcept; friend constexpr bool operator>=(/* 未指明 */ , partial_ordering v) noexcept; }; // 合法值的定义 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; // 仅用于阐释 // 仅用于阐释的构造函数 explicit constexpr weak_ordering(eq v) noexcept : value(int(v)) {} // 仅为说明 explicit constexpr weak_ordering(ord v) noexcept : value(int(v)) {} // 仅为说明 public: // 合法值 static const weak_ordering less; static const weak_ordering equivalent; static const weak_ordering greater; // 转换 constexpr operator weak_equality() const noexcept; constexpr operator partial_ordering() const noexcept; // 比较 friend constexpr bool operator==(weak_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator!=(weak_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator< (weak_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator<=(weak_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator> (weak_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator>=(weak_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator==(/* 未指明 */ , weak_ordering v) noexcept; friend constexpr bool operator!=(/* 未指明 */ , weak_ordering v) noexcept; friend constexpr bool operator< (/* 未指明 */ , weak_ordering v) noexcept; friend constexpr bool operator<=(/* 未指明 */ , weak_ordering v) noexcept; friend constexpr bool operator> (/* 未指明 */ , weak_ordering v) noexcept; friend constexpr bool operator>=(/* 未指明 */ , weak_ordering v) noexcept; }; // 合法值的定义 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; // 仅用于阐释 // 仅用于阐释的构造函数 explicit constexpr strong_ordering(eq v) noexcept : value(int(v)) {} // 仅用于阐释 explicit constexpr strong_ordering(ord v) noexcept : value(int(v)) {} // 仅用于阐释 public: // 合法值 static const strong_ordering less; static const strong_ordering equal; static const strong_ordering equivalent; static const strong_ordering greater; // 转换 constexpr operator weak_equality() const noexcept; constexpr operator strong_equality() const noexcept; constexpr operator partial_ordering() const noexcept; constexpr operator weak_ordering() const noexcept; // 比较 friend constexpr bool operator==(strong_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator!=(strong_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator< (strong_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator<=(strong_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator> (strong_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator>=(strong_ordering v, /* 未指明 */ ) noexcept; friend constexpr bool operator==(/* 未指明 */ , strong_ordering v) noexcept; friend constexpr bool operator!=(/* 未指明 */ , strong_ordering v) noexcept; friend constexpr bool operator< (/* 未指明 */ , strong_ordering v) noexcept; friend constexpr bool operator<=(/* 未指明 */ , strong_ordering v) noexcept; friend constexpr bool operator> (/* 未指明 */ , strong_ordering v) noexcept; friend constexpr bool operator>=(/* 未指明 */ , strong_ordering v) noexcept; }; // 合法值的定义 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); }

