namespace std {
template <class T, class Allocator>
bool operator==(const list<T, Allocator>& x, const list<T, Allocator>& y);
}
概要
listオブジェクトの等値比較を行う。
要件
型Tが==で比較可能であること。
効果
- C++03 :
distance(x.begin(), x.end()) == distance(y.begin(), y.end()) && equal(x.begin(), x.end(), y.begin()); - C++14 :
equal(x.begin(), x.end(), y.begin(), y.end());
戻り値
xとyの要素数および要素の値が等しければtrue、そうでなければfalseを返す。
計算量
線形時間。ただし、xとyのサイズが異なる場合は定数時間。
備考
- この演算子により、以下の演算子が使用可能になる (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
参照
- LWG Issue 2257. Simplify container requirements with the new algorithms
- C++14から、2つ目のイテレータ範囲のendイテレータをとる
equal()アルゴリズムを使用するようになった。
- C++14から、2つ目のイテレータ範囲のendイテレータをとる
- P1614R2 The Mothership has Landed
- C++20での三方比較演算子の追加と、関連する演算子の自動導出