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

履歴 編集

function template
<list>

std::operator==

namespace std {
  template <class T, class Allocator>
  bool operator==(const list<T, Allocator>& x, const list<T, Allocator>& y);
}

概要

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

要件

T==で比較可能であること。

効果

戻り値

xyの要素数および要素の値が等しければtrue、そうでなければfalseを返す。

計算量

線形時間。ただし、xyのサイズが異なる場合は定数時間。

備考

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

#include <iostream>
#include <list>

int main ()
{
  std::list<int> ls1 = {1, 2, 3};
  std::list<int> ls2 = {1, 2, 3};
  std::list<int> ls3 = {1, 2, 3, 4};

  std::cout << std::boolalpha;

  // 要素数と要素の値が等しい
  std::cout << (ls1 == ls2) << std::endl;

  // 要素の値は(左辺の要素数分まで)等しいが要素数が異なる
  std::cout << (ls1 == ls3) << std::endl;
}

出力

true
false

参照