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

履歴 編集

function template
<string>

std::operator==

namespace std {
  template <class CharT, class Traits, class Allocator>
  bool
    operator==(const basic_string<CharT, Traits, Allocator>& a,
               const basic_string<CharT, Traits, Allocator>& b);          // (1) C++03
  template <class CharT, class Traits, class Allocator>
  bool
    operator==(const basic_string<CharT, Traits, Allocator>& a,
               const basic_string<CharT, Traits, Allocator>& b) noexcept; // (1) C++14
  template <class CharT, class Traits, class Allocator>
  constexpr bool
    operator==(const basic_string<CharT, Traits, Allocator>& a,
               const basic_string<CharT, Traits, Allocator>& b) noexcept; // (1) C++20

  template <class CharT, class Traits, class Allocator>
  bool
    operator==(const basic_string<CharT, Traits, Allocator>& a,
               const CharT* b);                                  // (2) C++03
  template <class CharT, class Traits, class Allocator>
  constexpr bool
    operator==(const basic_string<CharT, Traits, Allocator>& a,
               const CharT* b);                                  // (2) C++20

  // (2)により、以下の演算子が使用可能になる (C++20)
  template <class CharT, class Traits, class Allocator>
  bool
    operator==(const CharT* a,
               const basic_string<CharT, Traits, Allocator>& b); // (3) C++03
  template <class CharT, class Traits, class Allocator>
  constexpr bool
    operator==(const CharT* a,
               const basic_string<CharT, Traits, Allocator>& b); // (3) C++20
}

概要

basic_stringオブジェクトの等値比較を行う。

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

事前条件

  • (2) パラメータbが、Traits::length(b) + 1の要素数を持つCharT文字型の配列を指していること

戻り値

備考

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

#include <iostream>
#include <string>

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

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

出力

equal

参照