还有其他几篇关于根据另一个向量 B 中的值对向量 A 进行排序的文章。大多数其他答案都告诉我们创建一个结构体或一个类以将值组合到一个对象中并使用 std::sort。
尽管我对此类解决方案的性能感到好奇,因为我需要优化实现冒泡排序的代码来对这两个向量进行排序。我正在考虑使用 vector>
并对其进行排序。
我正在开发一个斑点跟踪应用程序(图像分析),我尝试将以前跟踪的斑点与视频帧中新检测到的斑点进行匹配,其中我将每个帧与几个以前跟踪的帧以及我发现的斑点进行检查在之前的帧中。我以每秒 60 次的速度执行此操作(我的网络摄像头的速度)。
任何有关优化此问题的建议都将受到赞赏。我尝试优化的代码可以在此处显示:
http://code.google.com/p/projectknave/source/browse/trunk/knaveAddons/ofxBlobTracker/ofCvBlobTracker.cpp?spec=svn313&r=313
重要提示:我忘了提及向量的大小永远不会大于 5,并且大多数情况下只有 3 个项目,并且不会排序(也许我什至可以将其硬编码为 3 个项目?)
谢谢
There are a couple of other posts about sorting a vector A based on values in another vector B. Most of the other answers tell to create a struct or a class to combine the values into one object and use std::sort.
Though I'm curious about the performance of such solutions as I need to optimize code which implements bubble sort to sort these two vectors. I'm thinking to use a vector<pair<int,int>>
and sort that.
I'm working on a blob-tracking application (image analysis) where I try to match previously tracked blobs against newly detected blobs in video frames where I check each of the frames against a couple of previously tracked frames and of course the blobs I found in previous frames. I'm doing this at 60 times per second (speed of my webcam).
Any advice on optimizing this is appreciated. The code I'm trying to optimize can be shown here:
http://code.google.com/p/projectknave/source/browse/trunk/knaveAddons/ofxBlobTracker/ofCvBlobTracker.cpp?spec=svn313&r=313
important: I forgot to mention that the size of the vectors will never be bigger than 5, and mostly have only 3 items in it and will be unsorted (maybe I could even hardcode it for 3 items?)
Thanks
发布评论
评论(2)
C++ 提供了许多排序选项,从
std::sort
算法到std::map
和std::set
等排序容器。您应该始终尝试使用这些作为您的第一个解决方案,并且只尝试诸如“优化冒泡排序”之类的方法作为最后的手段。C++ provides lots of options for sorting, from the
std::sort
algorithm to sorted containers likestd::map
andstd::set
. You should always try to use these as your first solution, and only try things like "optimised bubble sorts" as a last resort.我不久前实现了这个。另外,我认为你的意思是以与
A 的排序值。
Index
包含data
的排序顺序。I implemented this a while ago. Also, I think you mean ordering a vector B in the same way as the
sorted values of A.
Index
contains the sorting order ofdata
.