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

履歴 編集

function
<iterator>

std::basic_const_iterator::operator==(C++23)

template<sentinel_for<Iterator> S>
constexpr bool operator==(const S& s) const;

概要

basic_const_iterator<Iterator>オブジェクトと別のイテレータ(basic_const_iterator<Iterator>Iteratorのオブジェクト)が同じ要素を指しているかを判定する。

効果

ラップしているイテレータをcurrent_メンバ変数に保持するとして、以下と等価

return current_ == s;

戻り値

2つのイテレータが同じ要素を指している場合にtrueを返す。

備考

この演算子により以下の演算子が使用可能になる。

// !=
template<sentinel_for<Iterator> S>
constexpr bool operator!=(const S&) const;

// 逆順
template<sentinel_for<Iterator> S>
friend constexpr bool operator==(const S&, const basic_const_iterator<Iterator>&);

template<sentinel_for<Iterator> S>
friend constexpr bool operator!=(const S&, const basic_const_iterator<Iterator>&);

#include <iostream>
#include <iterator>
#include <vector>

int main() {
  std::vector vec = {1, 2, 3, 4, 5};

  std::basic_const_iterator cit = vec.begin();
  auto se = vec.end();

  std::cout << std::boolalpha;

  // 元のイテレータとの比較
  std::cout << (cit == se) << '\n';
  std::cout << (se == cit) << '\n';

  std::basic_const_iterator cse = se;

  // basic_const_iterator同士の比較
  std::cout << (cit == cse) << '\n';
  std::cout << (cse == cit) << '\n';

  // !=の導出
  std::cout << (cit != se) << '\n';
  std::cout << (se != cit) << '\n';
  std::cout << (cit != cse) << '\n';
  std::cout << (cse != cit) << '\n';
}

出力

false
false
false
false
true
true
true
true

バージョン

言語

  • C++23

処理系

参照