Sort vectors based on size ()

i has a 2nd vector, such as vector < vector < coordinates > > v( points);where the class of coordinates:

class coordinate{
    public :
    int x;
    int y;
    coordinate(){
        x=0;
        y=0;
    }

};

and the points are 20. how to sort individual vectors v [i] based on v [i] .size (), that is, based on the number of coordinate objects inserted in v [i]. ???

+5
source share
2 answers

1) make a function that compares two vectors based on size:

bool less_vectors(const vector& a,const vector& b) {
   return a.size() < b.size();
}

2) sort with it

sort(v.begin(),v.end(),less_vectors);
+14
source
  • create a function in which you can use any attributes to compare objects, and then use the STL sorting algorithm to sort the container.

  • < , . sort(), STL (, STL).

0

All Articles