最終更新日時(UTC):
が更新

履歴 編集

function template
<string>

std::operator<=>(C++20)

namespace std {
  template <class charT, class traits, class Allocator>
    see below
  operator<=>(const basic_string<charT, traits, Allocator>& lhs,
              const basic_string<charT, traits, Allocator>& rhs) noexcept; // (1) C++20

  template <class charT, class traits, class Allocator>
    see below
  operator<=>(const basic_string<charT, traits, Allocator>& lhs,
              const charT* rhs);                                           // (2) C++20
}

概要

basic_stringオブジェクトの三方比較を行う。

デフォルトの比較では、大文字と小文字は区別される('a' == 'A'false)。
なお、この比較方法はchar_traitsによってカスタマイズでき、大文字・小文字を区別しない比較もできる。

戻り値

以下と等価:

return basic_string_view<charT, traits>(lhs) <=> basic_string_view<charT, traits>(rhs);

戻り値の型は、traits::comparison_categoryが存在していればその型、そうでなければweak_orderingとなる。

備考

  • この演算子により、以下の演算子が使用可能になる (C++20):
    • operator<
    • operator<=
    • operator>
    • operator>=

#include <iostream>
#include <string>

int main()
{
  std::string a = "abc";
  std::string b = "abc";

  if ((a <=> b) == 0) {
    std::cout << "equal" << std::endl;
  }
  else {
    std::cout << "not equal" << std::endl;
  }
}

出力

equal

参照