在两个std::向量之间匹配元素
match elements between two std::vectors
我正在编写一个估计光流的模块。在每个时间步长,它消耗一个std::向量,其中向量的每个元素都是当前像素位置和前一个像素位置。矢量未排序。以前未看到的新像素将出现,未找到的流位置将消失。是否有正确的方法将新矢量中的元素与正在估计的光流位置集相匹配?
矢量的数量级为2000个元素。
以下是我正在考虑的方法:
- 天真地迭代每个估计光流位置的新矢量
- 天真地迭代新的矢量,但删除每个匹配的位置,这样搜索就可以更快地进行
- 在每个时间步对我的列表和新列表运行std::sort。然后从最后匹配的索引+1开始迭代新向量
我怀疑有一种公认的方法可以做到这一点,但我没有受过任何compsci培训。
如果相关的话,我在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();
}
}
不确定你需要什么,但是,假设你的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 */ ;
}
}
};
其中Matcher
对象是用一个向量构造的,该向量用于初始化std::set
(或std::multi_set
,或无序集/多集?),以简化matchOpticalFlowNaive()
中的工作
相关文章:
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 在C++中查找两个向量之间最相似的值
- C++在 2 个向量向量之间交换向量
- 查找多个向量之间的公共元素(无整数元素)
- C 计算两个3D向量之间的角度(0至360)
- C++:快速/并行计算两个"std::vector<double>"向量之间的L1距离
- 计算三个或更多向量之间的向量
- 如何在C++中找到两个向量之间的最小(优化)距离
- 在两个std::向量之间匹配元素
- 获得 2 个向量之间最大差值的最快方法
- 检查两个向量<T>之间的差异
- C++11 - 2 个向量之间的emplace_back不起作用
- C++中两个向量之间的元素交换
- 计算两个向量之间共享元素数量的最快方法
- 两组向量之间的四元旋转
- 求两个向量之间的交集
- 在向量之间使用 std::swap 或 vector::swap
- 向量之间的交集
- 如何找到两个向量之间的角度
- 这是使用点积找到两个向量之间角度的正确方法吗?C++ SFML