简体   繁体   English

这两种比较STL载体的方法有什么区别?

[英]What is the difference between these two ways to compare STL vectors?

Few examples available online use the equality operator to compare the contents of two STL vector objects in order to verify that they have the same content. 在线可用的示例很少使用等于运算符来比较两个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;

Instead, I read other examples where the std::equal() function is used. 相反,我读了其他使用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());
}

What is the difference between these two ways to compare STL vectors? 这两种比较STL载体的方法有什么区别?

The two behave in exactly the same way. 两者表现完全相同。 The container requirements (Table 96) say that a == b has the operational semantics of: 容器要求(表96)表示a == b具有以下操作语义:

distance(a.begin(), a.end()) == distance(b.begin(), b.end()) &&
equal(a.begin(), a.end(), b.begin())

Good question. 好问题。 I suspect that people don't use == because they don't know its there, but it does exactly what the hand-coded version does. 我怀疑人们不使用==因为他们不知道它在那里,但它确实与手动编码版本完全相同。 It's always been there for sequence containers and for associative containers. 它始终存在于序列容器和关联容器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是什么导致两种构建static_vectors向量的方式之间的性能差异如此之大? - What causes so big performance difference between two ways of building vector of static_vectors? 通过两种方式声明二维向量的区别 - Difference between declaring 2d vectors by two ways 我们声明结构的两种方式有什么区别? - What is the difference between the two ways we declared a struct? 这两种调用基类复制赋值方法有什么区别? - What is the difference between these two ways to invoke base class copy assignment? 这两种 unordered_map 声明方式有什么区别? - What is the difference between these two ways of unordered_map declaration? 在C ++中的STL的比较函数中使用“ &lt;=”符号而不是“ &lt;”符号有什么区别? - What is the difference between using a “<=” sign instead of a “<” sign in the compare function of an STL in C++? 在向量和其他stl容器上进行迭代的样式之间的差异 - Difference between styles of iterating over vectors and other stl containers 比较CString的两个向量的最佳方法是什么 - What is the best method to compare two vectors of CString 插入地图的两种方式之间的差异 - difference between two ways to insert into map 在堆栈上声明对象的两种方式之间的区别 - Difference between two ways of declaring an object on the stack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM