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:
Then use
std::sortto sort according to that ordering: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):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 usestd::sortwith that comparator.