在 C++ 中比较向量的最快方法是什么?

What's the FASTEST way to compare vectors in C++?

在 C++ 中查看两个向量是否相等的最快方法是什么?

我正在尝试找到最快的方法来查看矩阵的任何行是否等于矩阵的任何列,因此逐个元素比较并在不相等时退出循环是不够的。

不要重新发明轮子。您可以使用 <algorithm>.

中的 std::equal

它的复杂度如下:

No applications of the corresponding predicate if InputIterator1 and InputIterator2 meet the requirements of random access iterators and last1 - first1 != last2 - first2. Otherwise, at most min(last1 - first1, last2 - first2) applications of the corresponding predicate.

这就是您要找的。
有关详细信息,请参阅文档。


如评论中所述,operator==std::equal 之间存在细微差别:如果类型不同,前者不起作用(例如,std::vector<int>std::vector<double>),后者确实有效。
我试图给出最通用的解决方案。
如果类型相同,当然 operator== 就像一个魅力,如 .

只需使用 operator == 向量:

std::vector<int> v1{1, 2, 3, 4}, v2{1, 2, 3, 4};
bool are_equal = (v1 == v2);

相等运算符 ( == ) 在 C++ Vector STL 中重载。所以你可以很容易地直接比较它们,就像比较两个整数一样。

要像你说的比较矩阵的行和列,使用循环并直接通过==比较行和列。