Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |utility‎ |tuple
       
       
      Utilities library
       
       
      Defined in header<tuple>
      template<class...TTypes,class...UTypes>

      bool operator==(conststd::tuple<TTypes...>& lhs,

                       conststd::tuple<UTypes...>& rhs);
      (1)(since C++11)
      (constexpr since C++14)
      template<class...TTypes,class...UTypes>

      bool operator!=(conststd::tuple<TTypes...>& lhs,

                       conststd::tuple<UTypes...>& rhs);
      (2)(since C++11)
      (constexpr since C++14)
      (until C++20)
      template<class...TTypes,class...UTypes>

      bool operator<(conststd::tuple<TTypes...>& lhs,

                     conststd::tuple<UTypes...>& rhs);
      (3)(since C++11)
      (constexpr since C++14)
      (until C++20)
      template<class...TTypes,class...UTypes>

      bool operator<=(conststd::tuple<TTypes...>& lhs,

                       conststd::tuple<UTypes...>& rhs);
      (4)(since C++11)
      (constexpr since C++14)
      (until C++20)
      template<class...TTypes,class...UTypes>

      bool operator>(conststd::tuple<TTypes...>& lhs,

                     conststd::tuple<UTypes...>& rhs);
      (5)(since C++11)
      (constexpr since C++14)
      (until C++20)
      template<class...TTypes,class...UTypes>

      bool operator>=(conststd::tuple<TTypes...>& lhs,

                       conststd::tuple<UTypes...>& rhs);
      (6)(since C++11)
      (constexpr since C++14)
      (until C++20)
      template<class...TTypes,class...UTypes>

      constexprstd::common_comparison_category_t<
          synth-three-way-result<TTypes, Elems>...>
          operator<=>(conststd::tuple<TTypes...>& lhs,

                       conststd::tuple<UTypes...>& rhs);
      (7)(since C++20)
      template<class...TTypes, tuple-like UTuple>
      constexprbool operator==(const tuple<TTypes...>& lhs,const UTuple& rhs);
      (8)(since C++23)
      template<class...TTypes, tuple-like UTuple>

      constexprstd::common_comparison_category_t<
          synth-three-way-result<TTypes,/* Elems */>...>

          operator<=>(const tuple<TTypes...>& lhs,const UTuple& rhs);
      (9)(since C++23)
      1,2) Compares every element of the tuplelhs with the corresponding element of the tuplerhs byoperator==.
      1) Returnstrue if all pairs of corresponding elements are equal.
      2) Returns!(lhs== rhs).
      Ifsizeof...(TTypes) does not equalsizeof...(UTypes), orstd::get<i>(lhs)== std::get<i>(rhs) is not a valid expression for anyi in[0sizeof...(Types)), the program is ill-formed.
      If the type and value category ofstd::get<i>(lhs)== std::get<i>(rhs) do not meet theBooleanTestable requirements for anyi in[0sizeof...(Types)), the behavior is undefined.
      (until C++26)
      This overload participates in overload resolution only ifsizeof...(TTypes) equalssizeof...(UTypes),std::get<i>(lhs)== std::get<i>(rhs) is a valid expression anddecltype(std::get<i>(lhs)== std::get<i>(rhs)) modelboolean-testable for everyi in[0sizeof...(Types)).
      (since C++26)
      3-6) Compareslhs andrhs lexicographically byoperator<, that is, compares the first elements, if they are equivalent, compares the second elements, if those are equivalent, compares the third elements, and so on.
      3) For empty tuples, returnsfalse. For non-empty tuples, the effect is equivalent to
      if(std::get<0>(lhs)< std::get<0>(rhs))returntrue;

      if(std::get<0>(rhs)< std::get<0>(lhs))returnfalse;
      if(std::get<1>(lhs)< std::get<1>(rhs))returntrue;
      if(std::get<1>(rhs)< std::get<1>(lhs))returnfalse;
      ...

      return std::get<N-1>(lhs)< std::get<N-1>(rhs);
      4) Returns!(rhs< lhs).
      5) Returnsrhs< lhs.
      6) Returns!(lhs< rhs).
      Ifsizeof...(TTypes) does not equalsizeof...(UTypes), or any of the comparison expression shown in the equivalent-to statements is not a valid expression, the program is ill-formed.
      If the type and value category of any of the comparison expression shown in the equivalent-to statements do not meet theBooleanTestable requirements, the behavior is undefined.
      7) Compareslhs andrhs lexicographically bysynth-three-way, that is, compares the first elements, if they are equivalent, compares the second elements, if those are equivalent, compares the third elements, and so on.

      if(auto c= synth-three-way(std::get<0>(lhs), std::get<0>(rhs)); c!=0)return c;
      if(auto c= synth-three-way(std::get<1>(lhs), std::get<1>(rhs)); c!=0)return c;
      ...
      return synth-three-way(std::get<N-1>(lhs), std::get<N-1>(rhs));

      8) Same as(1), except thatrhs is atuple-like object, and the number of elements ofrhs is determined bystd::tuple_size_v<UTuple> instead. This overload can only be found viaargument-dependent lookup.
      9) Same as(7), except thatrhs is atuple-like object./* Elems */ denotes the pack of typesstd::tuple_element_t<i, UTuple> for eachi in[0std::tuple_size_v<UTuple>) in increasing order. This overload can only be found viaargument-dependent lookup.

      All comparison operators are short-circuited; they do not access tuple elements beyond what is necessary to determine the result of the comparison.

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

      (since C++20)

      Contents

      [edit]Parameters

      lhs, rhs - tuples to compare

      [edit]Return value

      1,8)true ifstd::get<i>(lhs)== std::get<i>(rhs) for alli in[0sizeof...(Types)), otherwisefalse. For two empty tuples returnstrue.
      2)!(lhs== rhs)
      3)true if the first non-equivalent element inlhs is less than the one inrhs,false if the first non-equivalent element inrhs is less than the one inlhs or there is no non-equivalent element. For two empty tuples, returnsfalse.
      4)!(rhs< lhs)
      5)rhs< lhs
      6)!(lhs< rhs)
      7,9) The relation between the first pair of non-equivalent elements if there is any,std::strong_ordering::equal otherwise. For two empty tuples, returnsstd::strong_ordering::equal.

      [edit]Notes

      The relational operators are defined in terms of each element'soperator<.

      (until C++20)

      The relational operators are defined in terms ofsynth-three-way, which usesoperator<=> if possible, oroperator< otherwise.

      Notably, if an element 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)
      Feature-test macroValueStdFeature
      __cpp_lib_constrained_equality202403L(C++26)Constrainedoperator== forstd::tuple

      [edit]Example

      Becauseoperator< is defined for tuples, containers of tuples can be sorted.

      Run this code
      #include <algorithm>#include <iostream>#include <tuple>#include <vector> int main(){std::vector<std::tuple<int,std::string,float>> v{{2,"baz",-0.1},{2,"bar",3.14},{1,"foo",10.1},{2,"baz",-1.1},};std::sort(v.begin(), v.end()); for(constauto& p: v)std::cout<<"{ "<< get<0>(p)<<", "<< get<1>(p)<<", "<< get<2>(p)<<" }\n";}

      Output:

      { 1, foo, 10.1 }{ 2, bar, 3.14 }{ 2, baz, -1.1 }{ 2, baz, -0.1 }

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2114
      (P2167R3)
      C++11type preconditions for boolean operations were missingadded

      [edit]See also

      (removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)
      lexicographically compares the values in thepair
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/tuple/operator_cmp&oldid=175972"

      [8]ページ先頭

      ©2009-2025 Movatter.jp