排序矢量的双打矢量 [英] Sorting vector of vector of doubles

查看:143
本文介绍了排序矢量的双打矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以前的集群向量和当前集群的向量。一个集群有一个2D Point2F的向量,我想根据每个集群之间的距离,这是存储在距离向量,或者你可以建议一个更好的方法来排序这些集群向量?

I have vector of previous clusters, and a vector of current clusters. A cluster has a vector of 2D Point2F, I would like to sort out those clusters ascendly based on the distance between each cluster, which is stored in distance vector, or can you suggest a better way to sort the clusters vector ?

 distances.resize(previousClusters.size()*currentClusters.size());
         for (int i=0; i<previousClusters.size()*currentClusters.size(); i++)
         {
             distances[i].resize(previousClusters.size()*currentClusters.size());
         }

         for (int i=0; i< previousClusters.size(); i++)
         {

             for(int j=0; j < currentClusters.size(); j++)
             {

                 distances[i][j] = cv::norm(previousClusters[i].m_Properties.m_Center - currentClusters[j].m_Properties.m_Center );
             }
         }


推荐答案

:这回答了问题,因为它是最初写的(因为它仍然写在标题)。问题的主体已更改为无效,但答案仍然可以用于排序向量的向量。

NOTE: this answers the question as it was originally written (and as it still is written in the title). The body of the question has changed to invalidate it, but the answer might still be useful for sorting a vector of vectors.

首先,您需要决定它的含义一个向量在另一个之前排序,并写入比较器:

First, you need to decide what it means for one vector to be sorted before another, and write a comparator:

struct compare_distance_vectors {
    bool operator()(std::vector<double> const & v1, std::vector<double> const & v2) {
        // implement your comparison here.
        // return "true" if v1 should come before v2.
    }
};

然后使用 std :: sort 根据该顺序:

std::sort(vectors.begin(), vectors.end(), compare_distance_vectors());

如果你想要一个字典序那么可以使用默认比较器( std :: less< value_type> ),并使用< 比较):

If you want a lexicographical ordering (i.e. ordering by the first element, then by the second if that's equal, and so on), then you can use the default comparator (which is std::less<value_type>, and uses < to compare):

std::sort(vectors.begin(), vectors.end());

通常,对任何类型的序列进行排序(例如 std ::向量< cv :: Point2f> )根据任何顺序,写这样的比较器来指定排序,然后使用 std :: sort 与比较器。

Generally, to sort a sequence of any type (such as std::vector<cv::Point2f>) according to any ordering, write a comparator like that to specify the ordering, and then use std::sort with that comparator.

这篇关于排序矢量的双打矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆