Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::rel_ops::operator!=,>,<=,>=

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
      General utilities
      Relational operators(deprecated in C++20)
      rel_ops::operator!=rel_ops::operator>
        
      rel_ops::operator<=rel_ops::operator>=
      Integer comparison functions
      (C++20)(C++20)(C++20)  
      (C++20)
      Swap andtype operations
      (C++11)
      (C++11)
      (C++17)
      Common vocabulary types
      (C++11)
      (C++17)
      (C++17)
      (C++17)
      (C++11)
      (C++17)
      (C++23)


       
      Defined in header<utility>
      template<class T>
      bool operator!=(const T& lhs,const T& rhs);
      (1)(deprecated in C++20)
      template<class T>
      bool operator>(const T& lhs,const T& rhs);
      (2)(deprecated in C++20)
      template<class T>
      bool operator<=(const T& lhs,const T& rhs);
      (3)(deprecated in C++20)
      template<class T>
      bool operator>=(const T& lhs,const T& rhs);
      (4)(deprecated in C++20)

      Given a user-definedoperator== andoperator< for objects of typeT, implements the usual semantics of other comparison operators.

      1) Implementsoperator!= in terms ofoperator==.
      2) Implementsoperator> in terms ofoperator<.
      3) Implementsoperator<= in terms ofoperator<.
      4) Implementsoperator>= in terms ofoperator<.

      Contents

      [edit]Parameters

      lhs - left-hand argument
      rhs - right-hand argument

      [edit]Return value

      1) Returnstrue iflhs isnot equal torhs.
      2) Returnstrue iflhs isgreater thanrhs.
      3) Returnstrue iflhs isless or equal torhs.
      4) Returnstrue iflhs isgreater or equal torhs.

      [edit]Possible implementation

      (1)operator!=
      namespace rel_ops{template<class T>bool operator!=(const T& lhs,const T& rhs){return!(lhs== rhs);}}
      (2)operator>
      namespace rel_ops{template<class T>bool operator>(const T& lhs,const T& rhs){return rhs< lhs;}}
      (3)operator<=
      namespace rel_ops{template<class T>bool operator<=(const T& lhs,const T& rhs){return!(rhs< lhs);}}
      (4)operator>=
      namespace rel_ops{template<class T>bool operator>=(const T& lhs,const T& rhs){return!(lhs< rhs);}}

      [edit]Notes

      Boost.operators provides a more versatile alternative tostd::rel_ops.

      As of C++20,std::rel_ops are deprecated in favor ofoperator<=>.

      [edit]Example

      Run this code
      #include <iostream>#include <utility> struct Foo{int n;}; bool operator==(const Foo& lhs,const Foo& rhs){return lhs.n== rhs.n;} bool operator<(const Foo& lhs,const Foo& rhs){return lhs.n< rhs.n;} int main(){    Foo f1={1};    Foo f2={2};usingnamespace std::rel_ops; std::cout<<std::boolalpha<<"{1} != {2} : "<<(f1!= f2)<<'\n'<<"{1} >  {2} : "<<(f1>  f2)<<'\n'<<"{1} <= {2} : "<<(f1<= f2)<<'\n'<<"{1} >= {2} : "<<(f1>= f2)<<'\n';}

      Output:

      {1} != {2} : true{1} >  {2} : false{1} <= {2} : true{1} >= {2} : false
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/rel_ops/operator_cmp&oldid=152378"

      [8]ページ先頭

      ©2009-2025 Movatter.jp