Reputation: 249
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 );
}
}
Upvotes: 0
Views: 508
Reputation: 254471
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.
}
};
Then use std::sort
to sort according to that ordering:
std::sort(vectors.begin(), vectors.end(), compare_distance_vectors());
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());
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.
Upvotes: 4
Reputation: 2062
You can try to use std::sort on the "upper" vector (the second prototype allows you to define the way the "lower" vectors should be sorted) first, and then each time your function object Comp is called, use std::sort again on each "lower" method (with the first prototype being enough this time).
This can also work for your second question.
Upvotes: -1