在 C++ 中查看两个 vector 是否相等的最快方法是什么?
我试图找到最快的方法来查看矩阵的任何行是否等于矩阵的任何列,因此逐个元素比较并在不相等时退出循环是不够的。
最佳答案
不要重新发明轮子。您可以使用 std::equal
来自 <algorithm>
.
它具有以下复杂性:
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==
工作起来很有魅力,因为mentioned by @Jarod42 .
关于c++ - 在 C++ 中比较 vector 的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40372423/