オンラインで入手できるいくつかの例では、等値演算子を使用して 2 つの STLvector
オブジェクトの内容を比較し、それらの内容が同じであることを確認しています。
vector<T> v1;
// add some elements to v1
vector<T> v2;
// add some elements to v2
if (v1 == v2) cout << "v1 and v2 have the same content" << endl;
else cout << "v1 and v2 are different" << endl;
代わりに、std::equal()
関数が使用されている他の例を読みました。
bool compare_vector(const vector<T>& v1, const vector<T>& v2)
{
return v1.size() == v2.size()
&& std::equal(v1.begin(), v1.end(), v2.begin());
}
STL ベクトルを比較するこれら 2 つの方法の違いは何ですか?