简体   繁体   English

匹配两个std :: vectors之间的元素

[英]match elements between two std::vectors

I am writing a module that estimates optical flow. 我正在编写一个估算光流量的模块。 At each time step it consumes an std::vector where each element of the vector is a current pixel location and a previous pixel location. 在每个时间步上,它消耗一个std :: vector,其中向量的每个元素都是当前像素位置和先前像素位置。 The vector is not ordered. 该向量未排序。 New pixels that were previously not seen will be present and flow locations that were not found will be gone. 将会出现以前看不见的新像素,而找不到的流位置将消失。 Is there a correct way to match elements in the new vector to the set of optical flow locations being estimated? 是否有正确的方法将新矢量中的元素与要估计的一组光流位置进行匹配?

The vectors are on the order of 2000 elements. 向量约为2000个元素。

These are the approaches I am considering: 这些是我正在考虑的方法:

  • naively iterate through the new vector for each estimated optical flow location 对于每个估计的光流位置,天真地遍历新矢量
  • naively iterating through the new vector but removing each matched location so the search gets faster as it goes on 天真地遍历新向量,但删除了每个匹配的位置,因此随着搜索的进行,搜索变得更快
  • run std::sort on my list and the new list at every time step. 在每个时间步上在我的列表和新列表上运行std :: sort。 Then iterate through the new vector starting at the last matched index +1 然后从最后一个匹配索引+1开始遍历新向量

I'm suspecting that there is an accepted way to go about this but I don't have any comp sci training. 我怀疑有解决此问题的公认方法,但是我没有任何专业科学训练。

I'm in c++ 11 if that is relevant. 如果相关,我在c ++ 11中。

// each element in the new vector is an int. I need to check if 
// there are matches between the new vec and old vec
void Matcher::matchOpticalFlowNaive(std::vector<int> new_vec)
{
for(int i = 0; i < this->old_vec.size(); i++)
    for(int j =0; j < new_vec.size(); j++)
        if(this->old_vec[i] == new_vec[j]){
            do_stuff(this->old_vec[i],  new_vec[j])
            j = new_vec.size();
        }
}

Not sure to understand what do you need but, supposing that your Matcher is constructed with a vector of integer, that there ins't important the order and that you need check this vector with other vectors (method matchOpticalFlowNaive() ) to do something when there is a match, I suppose you can write something as follows 不知道您需要什么,但是假设您的Matcher是用整数向量构造的,那么顺序并不重要,并且您需要与其他向量(方法matchOpticalFlowNaive() )一起检查该向量以在需要时进行处理有一个匹配项,我想您可以编写如下内容

struct Matcher
 {
   std::set<int> oldSet;

   Matcher (std::vector<int> const & oldVect)
      : oldSet{oldVect.cbegin(), oldVect.cend()}
    { }

   void matchOpticalFlowNaive (std::vector<int> const & newVec)
    {
      for ( auto const & vi : newVec )
       {
         if ( oldSet.cend() != oldSet.find(vi) )
            /* do something */ ;
       }
    }
 };

where the Matcher object is constructed with a vector that is used to initialize a std::set (or a std::multi_set , or a unordered set/multiset?) to make simple the work in matchOpticalFlowNaive() 其中Matcher对象是使用向量构造的,该向量用于初始化std::set (或std::multi_set或无序set / multiset?),以简化matchOpticalFlowNaive()的工作

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM