C++ 参考手册

位置:首页 > C++ 参考手册 >容器库 >std::array > operator==,!=,<,<=,>,>=,<=>(std::array)

定义于头文件 <array>
(1)
template< class T, std::size_t N >

bool operator==( const std::array<T,N>& lhs,

                 const std::array<T,N>& rhs );
(C++20 前)
template< class T, std::size_t N >

constexpr bool operator==( const std::array<T,N>& lhs,

                           const std::array<T,N>& rhs );
(C++20 èµ·)
template< class T, std::size_t N >

bool operator!=( const std::array<T,N>& lhs,

                 const std::array<T,N>& rhs );
(2) (C++20 前)
template< class T, std::size_t N >

bool operator<( const std::array<T,N>& lhs,

                const std::array<T,N>& rhs );
(3) (C++20 前)
template< class T, std::size_t N >

bool operator<=( const std::array<T,N>& lhs,

                 const std::array<T,N>& rhs );
(4) (C++20 前)
template< class T, std::size_t N >

bool operator>( const std::array<T,N>& lhs,

                const std::array<T,N>& rhs );
(5) (C++20 前)
template< class T, std::size_t N >

bool operator>=( const std::array<T,N>& lhs,

                 const std::array<T,N>& rhs );
(6) (C++20 前)
template< class T, std::size_t N >

constexpr /* see below */ operator<=>( const std::array<T,N>& lhs,

                                       const std::array<T,N>& rhs );
(7) (C++20 èµ·)

比较二个 array 的内容。

1-2) 检查 lhs 与 rhs 的内容是否相等,即它们是否拥有相同数量的元素且 lhs 中每个元素与 rhs 的同位置元素比较相等。
3-6) 按字典序比较 lhs 与 rhs 的内容。由等价于 std::lexicographical_compare 的函数进行比较。
7) 按字典序比较 lhs 与 rhs 的内容。如同通过在二个 array 上以进行合成三路比较(见后述)的函数对象调用 std::lexicographical_compare_three_way 进行比较。返回类型同合成三路比较的结果类型。

给定分别作为左操作数与右操作数的两个 const E 左值 lhs 与 rhs (其中 E 为 T ),合成三路比较定义如下:

  • è‹¥ std::three_way_comparable_with<E, E> 得到满足则等价于 lhs <=> rhs ï¼›
  • 否则,若以 operator< 比较二个 const E 左值为良构且结果类型满足 boolean-testable ,则等价于
lhs < rhs ? std::weak_ordering::less :
rhs < lhs ? std::weak_ordering::greater :
            std::weak_ordering::equivalent
  • 否则,不定义合成三路比较,而 operator<=> 不参与重载决议。
若 three_way_comparable_with 或 boolean-testable 被满足但未被实现,或使用 operator< 但 E 与 < 不建立全序,则 operator<=> 的行为未定义。

参数

lhs, rhs - 要比较内容的 array
-
为使用重载 (1-2) , T 必须满足可相等比较 (EqualityComparable) 的要求。
-
为使用重载 (3-6) , T 必须满足可小于比较 (LessThanComparable) 的要求。顺序关系必须建立全序。

返回值

1) 若 array 内容相等则为 true ,否则为 false
2) 若 array 内容不相等则为 true ,否则为 false
3) 若 lhs 的内容按字典序小于 rhs 的内容则为 true ,否则为 false
4) 若 lhs 的内容按字典序小于或等于 rhs 的内容则为 true ,否则为 false
5) 若 lhs 的内容按字典序大于 rhs 的内容则为 true ,否则为 false
6) 若 lhs 的内容按字典序大于或等于 rhs 的内容则为 true ,否则为 false
7) 若 lhs 的内容按字典序小于 rhs 的内容则为 std::strong_ordering::less ;
若 lhs 的内容按字典序大于 rhs 的内容则为 std::strong_ordering::greater ;
若 lhs 与 rhs 中的首对不等价元素无序则为 std::partial_ordering::unordered ;
否则为 std::strong::equal 。

复杂度

与 array 大小成线性