Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |container‎ |array

      [edit template]
       
       
       
       
      Defined in header<array>
      template<class T,std::size_t N>

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

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

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

                       conststd::array<T, N>& rhs);
      (2)(since C++11)
      (until C++20)
      template<class T,std::size_t N>

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

                       conststd::array<T, N>& rhs);
      (3)(since C++11)
      (until C++20)
      template<class T,std::size_t N>

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

                       conststd::array<T, N>& rhs);
      (4)(since C++11)
      (until C++20)
      template<class T,std::size_t N>

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

                       conststd::array<T, N>& rhs);
      (5)(since C++11)
      (until C++20)
      template<class T,std::size_t N>

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

                       conststd::array<T, N>& rhs);
      (6)(since C++11)
      (until C++20)
      template<class T,std::size_t N>

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

                       conststd::array<T, N>& rhs);
      (7)(since C++20)

      Compares the contents of twoarrays.

      Letvalue_type be the value type ofarray (i.e.,typename array::value_type):

      1,2) Checks if the contents oflhs andrhs are equal, that is, they have the same number of elements and each element inlhs compares equal with the element inrhs at the same position.
      Equivalent to:

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

      (until C++14)

      returnstd::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());

      (since C++14)
      Ifvalue_type is notEqualityComparable, the behavior is undefined.
      3-7) Compares the contents oflhs andrhs lexicographically.
      3-6) Equivalent toreturnstd::lexicographical_compare(lhs.begin(), lhs.end(),
                                          rhs.begin(), rhs.end());
      .
      If any of the following conditions is satisfied, the behavior is undefined:
      7) Equivalent toreturnstd::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
                                                    rhs.begin(), rhs.end(),
                                                    synth-three-way)
      .
      The return type is the return type ofsynth-three-way (i.e.,synth-three-way-result <value_type>).
      If any of the following conditions is satisfied, the behavior is undefined:
      • T does not modelthree_way_comparable.
      • operator< is not defined for values of type (possibly const-qualified)value_type.
      • operator< does not establishtotal order.

      The<,<=,>,>=, and!= operators aresynthesized fromoperator<=> andoperator== respectively.

      (since C++20)

      Contents

      [edit]Parameters

      lhs, rhs -arrays whose contents to compare

      [edit]Return value

      Operatorlhs andrhs
      are equal
      lhs is
       lexicographically greater 
      rhs is
       lexicographically greater 
      operator==truefalse
      operator!=falsetrue
      operator<falsefalsetrue
      operator<=true
      operator>falsetruefalse
      operator>=true
       operator<=>  a value equal to0 a value greater then0a value less than0

      [edit]Complexity

      Linear in the size of thearray.

      [edit]Notes

      The relational operators are defined in terms ofvalue_type'soperator<.

      (until C++20)

      The relational operators are not defined. The rewritten candidateoperator<=> will be selected by overload resolution.

      operator<=> usesvalue_type'soperator<=> if possible, orvalue_type'soperator< otherwise. Notably, if thevalue_type does not itself provideoperator<=>, but is implicitly convertible to a three-way comparable type, that conversion will be used instead ofoperator<.

      (since C++20)

      [edit]Example

      Run this code
      #include <cassert>#include <compare>#include <array> int main(){conststd::array        a{1,2,3},        b{1,2,3},        c{7,8,9}; assert("""Compare equal containers:"&&(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&& "Compare non equal containers:"&&(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&&"");}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3431C++20operator<=> did not requireT
      to modelthree_way_comparable
      requires
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/array/operator_cmp&oldid=116774"

      [8]ページ先頭

      ©2009-2025 Movatter.jp