检查元素是否在两个向量中的最快方法

Fastest way to check if element is in both vectors

本文关键字:向量 方法 两个 是否 元素 检查      更新时间:2023-10-16

所以,认为我们有两个向量,vec1 和 vec2。仅对两个向量中的元素执行某些操作的最快方法是什么。到目前为止,我已经做到了。简单地说,我们如何才能更快地实现这一目标,或者有什么办法:

vector<Test*> vec1;
vector<Test*> vec2;
//Fill both of the vectors, with vec1 containing all existing 
//objects of Test, and vec2 containing some of them.

for (Test* test : vec1){
    //Check if test is in vec2
    if (std::find(vec2.begin(), vec2.end(), test) != vec2.end){
        //Do some stuff
    }
}

你的方法是 O(M*N),因为它调用std::find每个元素的 vec2 元素数量的线性vec1。您可以通过多种方式对其进行改进:

  • 排序vec2可以让您将时间减少到 O((N+M)*Log M) - 即您可以在范围上使用二叉搜索vec2.begin(), vec2.end()
  • 对两个向量进行排序可以让您在 O(N LogN + M Log M中搜索) - 您可以使用类似于合并排序范围的算法来查找线性时间中的匹配对
  • 对元素使用哈希集vec2可以让您将时间减少到 O(N+M) - 现在集合的构造时间和其中的搜索都是线性的。

一个简单的方法是std::unordered_set

vector<Test*> vec1;
vector<Test*> vec2;
//Fill both of the vectors, with vec1 containing all existing 
//objects of Test, and vec2 containing some of them.
std::unordered_set<Test*> set2(vec2.begin(),vec2.end());
for (Test* t : vec1) {
   //O(1) lookup in hash set
   if (set2.find(t)!=set2.end()) {
     //stuff
    }
 }

O(n+m),其中 n 是 vec1 中的元素数,m 是 vec2 中的元素数 }