GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::operator<=> - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. string
  3. basic_string
  4. operator<=>

最終更新日時: 2025年07月14日 23時04分00秒 (JST)
Koichi Murase が更新

履歴 編集

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

参照

  • P1614R2 The Mothership has Landed
    • C++20での三方比較演算子の追加と、関連する演算子の自動導出
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.