很少有可用的在线示例使用相等运算符来比较两个STL对象的内容,以验证它们是否具有相同的内容。这两种比较STL向量的方式有什么区别?
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向量的方法有什么区别?
没有区别。 – 2013-03-06 23:10:59