c++根据其他向量的值对向量进行排序,或者哪个更快?

发布于 2024-09-04 12:21:53 字数 793 浏览 2 评论 0 原文

还有其他几篇关于根据另一个向量 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

束缚m 2024-09-11 12:21:53

C++ 提供了许多排序选项,从 std::sort 算法到 std::mapstd::set 等排序容器。您应该始终尝试使用这些作为您的第一个解决方案,并且只尝试诸如“优化冒泡排序”之类的方法作为最后的手段。

C++ provides lots of options for sorting, from the std::sort algorithm to sorted containers like std::map and std::set. You should always try to use these as your first solution, and only try things like "optimised bubble sorts" as a last resort.

著墨染雨君画夕 2024-09-11 12:21:53

我不久前实现了这个。另外,我认为你的意思是以与
A 的排序值。

Index 包含data 的排序顺序。

/** Sorts a vector and returns index of the sorted values
 * \param Index Contains the index of sorted values in the original vector
 * \param data The vector to be sorted
 */
template<class T>
void paired_sort(vector<unsigned int> & Index, const vector<T> & data)
{
    // A vector of a pair which will contain the sorted value and its index in the original array
    vector<pair<T,unsigned int>> IndexedPair;
    IndexedPair.resize(data.size());
    for(unsigned int i=0;i<IndexedPair.size();++i)
    {
        IndexedPair[i].first = data[i];
        IndexedPair[i].second = i;
    }
    sort(IndexedPair.begin(),IndexedPair.end());
    Index.resize(data.size());
    for(size_t i = 0; i < Index.size(); ++i) Index[i] = IndexedPair[i].second;
}

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 of data.

/** Sorts a vector and returns index of the sorted values
 * \param Index Contains the index of sorted values in the original vector
 * \param data The vector to be sorted
 */
template<class T>
void paired_sort(vector<unsigned int> & Index, const vector<T> & data)
{
    // A vector of a pair which will contain the sorted value and its index in the original array
    vector<pair<T,unsigned int>> IndexedPair;
    IndexedPair.resize(data.size());
    for(unsigned int i=0;i<IndexedPair.size();++i)
    {
        IndexedPair[i].first = data[i];
        IndexedPair[i].second = i;
    }
    sort(IndexedPair.begin(),IndexedPair.end());
    Index.resize(data.size());
    for(size_t i = 0; i < Index.size(); ++i) Index[i] = IndexedPair[i].second;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文