Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |utility‎ |pair
       
       
      Utilities library
       
      std::pair
      Member functions
      (C++11)
      Non-member functions
      operator==operator!=operator<operator<=operator>operator>=operator<=>
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      Helper classes
      Deduction guides(C++17)
       
      Defined in header<utility>
      (1)
      template<class T1,class T2,class U1,class U2>
      bool operator==(conststd::pair<T1, T2>& lhs,conststd::pair<U1, U2>& rhs);
      (until C++14)
      template<class T1,class T2,class U1,class U2>

      constexprbool operator==(conststd::pair<T1, T2>& lhs,

                                 conststd::pair<U1, U2>& rhs);
      (since C++14)
      (2)
      template<class T1,class T2,class U1,class U2>
      bool operator!=(conststd::pair<T1, T2>& lhs,conststd::pair<U1, U2>& rhs);
      (until C++14)
      template<class T1,class T2,class U1,class U2>

      constexprbool operator!=(conststd::pair<T1, T2>& lhs,

                                 conststd::pair<U1, U2>& rhs);
      (since C++14)
      (until C++20)
      (3)
      template<class T1,class T2,class U1,class U2>
      bool operator<(conststd::pair<T1, T2>& lhs,conststd::pair<U1, U2>& rhs);
      (until C++14)
      template<class T1,class T2,class U1,class U2>

      constexprbool operator<(conststd::pair<T1, T2>& lhs,

                               conststd::pair<U1, U2>& rhs);
      (since C++14)
      (until C++20)
      (4)
      template<class T1,class T2,class U1,class U2>
      bool operator<=(conststd::pair<T1, T2>& lhs,conststd::pair<U1, U2>& rhs);
      (until C++14)
      template<class T1,class T2,class U1,class U2>

      constexprbool operator<=(conststd::pair<T1, T2>& lhs,

                                 conststd::pair<U1, U2>& rhs);
      (since C++14)
      (until C++20)
      (5)
      template<class T1,class T2,class U1,class U2>
      bool operator>(conststd::pair<T1, T2>& lhs,conststd::pair<U1, U2>& rhs);
      (until C++14)
      template<class T1,class T2,class U1,class U2>

      constexprbool operator>(conststd::pair<T1, T2>& lhs,

                               conststd::pair<U1, U2>& rhs);
      (since C++14)
      (until C++20)
      (6)
      template<class T1,class T2,class U1,class U2>
      bool operator>=(conststd::pair<T1, T2>& lhs,conststd::pair<U1, U2>& rhs);
      (until C++14)
      template<class T1,class T2,class U1,class U2>

      constexprbool operator>=(conststd::pair<T1, T2>& lhs,

                                 conststd::pair<U1, U2>& rhs);
      (since C++14)
      (until C++20)
      template<class T1,class T2,class U1,class U2>

      constexprstd::common_comparison_category_t<synth-three-way-result<T1, U1>,
                                                  synth-three-way-result<T2, U2>>

          operator<=>(conststd::pair<T1, T2>& lhs,conststd::pair<U1, U2>& rhs);
      (7)(since C++20)
      1,2) Tests if both elements oflhs andrhs are equal, that is, compareslhs.first withrhs.first andlhs.second withrhs.second.

      The behavior is undefined if the type and value category of eitherlhs.first== rhs.first orlhs.second== rhs.second do not meet theBooleanTestable requirements.

      (until C++26)

      This overload participates in overload resolution only if bothdecltype(lhs.first== rhs.first) anddecltype(lhs.second== rhs.second) modelboolean-testable.

      (since C++26)
      3-6) Compareslhs andrhs lexicographically byoperator<, that is, compares the first elements and only if they are equivalent, compares the second elements. The behavior is undefined if the type and value category of any oflhs.first< rhs.first,rhs.first< lhs.first, orlhs.second< rhs.second do not meet theBooleanTestable requirements.
      7) Compareslhs andrhs lexicographically bysynth-three-way, that is, compares the first elements and only if they are equivalent, compares the second elements.synth-three-way-result is the return type ofsynth-three-way.

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

      (since C++20)

      Contents

      [edit]Parameters

      lhs, rhs - pairs to compare

      [edit]Return value

      1)true if bothlhs.first== rhs.first andlhs.second== rhs.second, otherwisefalse.
      2)!(lhs== rhs)
      3) Iflhs.first< rhs.first, returnstrue. Otherwise, ifrhs.first< lhs.first, returnsfalse. Otherwise, iflhs.second< rhs.second, returnstrue. Otherwise, returnsfalse.
      4)!(rhs< lhs)
      5)rhs< lhs
      6)!(lhs< rhs)
      7)synth-three-way(lhs.first, rhs.first) if it is not equal to0, otherwisesynth-three-way(lhs.second, rhs.second).

      [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::pair

      [edit]Example

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

      Run this code
      #include <algorithm>#include <iomanip>#include <iostream>#include <string>#include <utility>#include <vector> int main(){std::vector<std::pair<int,std::string>> v={{2,"baz"},{2,"bar"},{1,"foo"}};std::sort(v.begin(), v.end()); for(auto p: v)std::cout<<'{'<< p.first<<", "<<std::quoted(p.second)<<"}\n";}

      Output:

      {1, "foo"}{2, "bar"}{2, "baz"}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 296C++98the descriptions of operators other than== and< were missingadded
      LWG 2114
      (P2167R3)
      C++98type preconditions for boolean operations were missingadded
      LWG 3865C++98comparison operators only acceptedpairs of the same typeacceptpairs of different types

      [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 the tuple
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/pair/operator_cmp&oldid=175970"

      [8]ページ先頭

      ©2009-2025 Movatter.jp