在线上很少能找到使用等号运算符来比较两个STL vector
对象的内容以验证它们是否相同的示例。
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 vector的方式有何区别?