operator==,!=,<,<=,>,>=,<=>(std::array)

来自cppreference.com
< cpp‎ | container‎ | array
 
 
 
 
在标头 <array> 定义
template< class T, std::size_t N >

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

                 const std::array<T, N>& rhs );
(1) (C++11 起)
(C++20 起为 constexpr)
template< class T, std::size_t N >

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

                 const std::array<T, N>& rhs );
(2) (C++11 起)
(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++11 起)
(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++11 起)
(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++11 起)
(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++11 起)
(C++20 前)
template< class T, std::size_t N >

/* 见下文 */
    operator<=>( const std::array<T, N>& lhs,

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

比较两个 array 的内容。

value_typearray 的值类型(即 typename array::value_type):

1,2) 检查 lhsrhs 的内容是否相等,即它们是否拥有相同数量的元素且 lhs 中每个元素与 rhs 的同位置元素比较相等。
等价于:

return std::distance(lhs.begin(), lhs.end())
               == std::distance(rhs.begin(), rhs.end())
           && std::equal(lhs.begin(), lhs.end(), rhs.begin());

(C++14 前)

return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());

(C++14 起)
如果 value_type可相等比较 (EqualityComparable) ,那么行为未定义。
3-7) 按字典序比较 lhsrhs 的内容。
3-6) 等价于 return std::lexicographical_compare(lhs.begin(), lhs.end(),
                                    rhs.begin(), rhs.end());
如果满足以下任意条件,那么行为未定义:
7) 等价于 return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
                                              rhs.begin(), rhs.end(),
                                              synth-three-way)
返回类型是 synth-three-way 的返回类型(即 synth-three-way-result <value_type>)。
如果满足以下任意条件,那么行为未定义:
  • T 未实现 three_way_comparable
  • 没有为(可有 const 限定的)value_type 类型的值定义 operator<
  • operator< 未建立全序

<<=>>=!= 运算符分别从 operator<=>operator== 合成

(C++20 起)

目录

[编辑] 参数

lhs, rhs - 要比较内容的 array

[编辑] 返回值

运算符  lhsrhs 相等   lhs 在字典序上更大   rhs 在字典序上更大 
operator== true false
operator!= false true
operator< false false true
operator<= true
operator> false true false
operator>= true
 operator<=>  等于 0 的某个值 大于 0 的某个值 小于 0 的某个值

[编辑] 复杂度

array 的大小成线性。

[编辑] 注解

各关系运算符是基于 value_typeoperator< 定义的。

(C++20 前)

不定义关系运算符。此时重载决议会选择重写候选 operator<=>

operator<=> 在有可能时会使用 value_typeoperator<=>,否则使用 value_typeoperator<。要注意,如果 value_type 自身不提供 operator<=>,但可以隐式转换为可三路比较的类型,那么就会用这项转换来替代 operator<

(C++20 起)

[编辑] 示例

#include <cassert>
#include <compare>
#include <array>
 
int main()
{
    const std::array
        a{1, 2, 3},
        b{1, 2, 3},
        c{7, 8, 9};
 
    assert
    (""
        "比较相等的容器:" &&
        (a != b) == false &&
        (a == b) == true &&
        (a < b) == false &&
        (a <= b) == true &&
        (a > b) == false &&
        (a >= b) == true &&
        (a <=> b) != std::weak_ordering::less &&
        (a <=> b) != std::weak_ordering::greater &&
        (a <=> b) == std::weak_ordering::equivalent &&
        (a <=> b) >= 0 &&
        (a <=> b) <= 0 &&
        (a <=> b) == 0 &&
 
        "比较不相等的容器:" &&
        (a != c) == true &&
        (a == c) == false &&
        (a < c) == true &&
        (a <= c) == true &&
        (a > c) == false &&
        (a >= c) == false &&
        (a <=> c) == std::weak_ordering::less &&
        (a <=> c) != std::weak_ordering::equivalent &&
        (a <=> c) != std::weak_ordering::greater &&
        (a <=> c) < 0 &&
        (a <=> c) != 0 &&
        (a <=> c) <= 0 &&
    "");
}

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 3431 C++20 operator<=> 不需要 T 实现 three_way_comparable 需要