(1) template <class T, size_T N>
bool operator== ( const array<T,N>& lhs, const array<T,N>& rhs );
(2) template <class T, size_T N>
bool operator!= ( const array<T,N>& lhs, const array<T,N>& rhs );
(3) template <class T, size_T N>
bool operator< ( const array<T,N>& lhs, const array<T,N>& rhs );
(4) template <class T, size_T N>
bool operator<= ( const array<T,N>& lhs, const array<T,N>& rhs );
(5) template <class T, size_T N>
bool operator> ( const array<T,N>& lhs, const array<T,N>& rhs );
(6) template <class T, size_T N>
bool operator>= ( const array<T,N>& lhs, const array<T,N>& rhs );
相等比较(operator==)通过使用operator==顺序比较元素来执行,
在第一个不匹配处停止(就像使用algorithm equal一样)
小于比较(operator<)的行为就像使用运算法则lexicographal_compare,
它使用operator<以对等的方式顺序比较元素(即,检查a<b和b<a),并在第一次出现时停止.
// array comparisons
#include <iostream>
#include <array>
int main ()
{
std::array<int,5> a = {10, 20, 30, 40, 50};
std::array<int,5> b = {10, 20, 30, 40, 50};
std::array<int,5> c = {50, 40, 30, 20, 10};
if (a==b) std::cout << "a and b are equal\n";
if (b!=c) std::cout << "b and c are not equal\n";
if (b<c) std::cout << "b is less than c\n";
if (c>b) std::cout << "c is greater than b\n";
if (a<=b) std::cout << "a is less than or equal to b\n";
if (a>=b) std::cout << "a is greater than or equal to b\n";
return 0;
} |