Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
      Constants
      Non-member functions
      I/O
      Comparison
      operator==operator!=operator<operator>operator<=operator>=operator<=>
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      Numeric conversions
      (C++11)(C++11)(C++11)
      (C++11)(C++11) 
      (C++11)(C++11)(C++11)
      (C++11)
      (C++11)
      Literals
      Helper classes
      Deduction guides(C++17)
       
      Defined in header<string>
      Compare twobasic_string objects
      template<class CharT,class Traits,class Alloc>

      bool operator==(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                       conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (1)(noexcept since C++11)
      (constexpr since C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator!=(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                       conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (2)(until C++20)
      (noexcept since C++11)
      template<class CharT,class Traits,class Alloc>

      bool operator<(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                     conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (3)(until C++20)
      (noexcept since C++11)
      template<class CharT,class Traits,class Alloc>

      bool operator<=(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                       conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (4)(until C++20)
      (noexcept since C++11)
      template<class CharT,class Traits,class Alloc>

      bool operator>(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                     conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (5)(until C++20)
      (noexcept since C++11)
      template<class CharT,class Traits,class Alloc>

      bool operator>=(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                       conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (6)(until C++20)
      (noexcept since C++11)
      template<class CharT,class Traits,class Alloc>

      constexpr/*comp-cat*/
          operator<=>(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                       conststd::basic_string<CharT,Traits,Alloc>& rhs)noexcept;
      (7)(since C++20)
      Compare abasic_string object and null-terminated array ofT
      template<class CharT,class Traits,class Alloc>

      bool operator==(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                       const CharT* rhs);
      (8)(constexpr since C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator==(const CharT* lhs,

                       conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (9)(until C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator!=(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                       const CharT* rhs);
      (10)(until C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator!=(const CharT* lhs,

                       conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (11)(until C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator<(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                     const CharT* rhs);
      (12)(until C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator<(const CharT* lhs,

                     conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (13)(until C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator<=(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                       const CharT* rhs);
      (14)(until C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator<=(const CharT* lhs,

                       conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (15)(until C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator>(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                     const CharT* rhs);
      (16)(until C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator>(const CharT* lhs,

                     conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (17)(until C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator>=(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                       const CharT* rhs);
      (18)(until C++20)
      template<class CharT,class Traits,class Alloc>

      bool operator>=(const CharT* lhs,

                       conststd::basic_string<CharT,Traits,Alloc>& rhs);
      (19)(until C++20)
      template<class CharT,class Traits,class Alloc>

      constexpr/*comp-cat*/
          operator<=>(conststd::basic_string<CharT,Traits,Alloc>& lhs,

                       const CharT* rhs);
      (20)(since C++20)

      Compares the contents of a string with another string or a null-terminated array ofCharT.

      All comparisons are done via thecompare() member function (which itself is defined in terms ofTraits::compare()):

      • Two strings are equal if both the size oflhs andrhs are equal and each character inlhs has equivalent character inrhs at the same position.
      1-7) Compares twobasic_string objects.
      8-20) Compares abasic_string object and a null-terminated array ofCharT.

      The return type of three-way comparison operators (/*comp-cat*/) isTraits::comparison_category if that qualified-id exists and denotes a type,std::weak_ordering otherwise. If/*comp-cat*/ is not a comparison category type, the program is ill-formed.

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

      (since C++20)

      Contents

      [edit]Parameters

      lhs, rhs - strings whose contents to compare

      [edit]Return value

      1-6,8-19)true if the corresponding comparison holds,false otherwise.
      7,20)static_cast</*comp-cat*/>(lhs.compare(rhs)<=>0).

      [edit]Complexity

      Linear in the size of the strings.

      [edit]Notes

      If at least one parameter is of typestd::string,std::wstring,std::u8string,std::u16string, orstd::u32string, the return type ofoperator<=> isstd::strong_ordering.

      (since C++20)

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2064C++11whether overloads taking twobasic_strings are noexcept was inconsistent;
      overloads taking aCharT* were noexcept but might raise UB
      made consistent;
      noexcept removed
      LWG 3432C++20the return type ofoperator<=> was not required to be a comparison category typerequired
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/operator_cmp&oldid=171034"

      [8]ページ先頭

      ©2009-2025 Movatter.jp