Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting vectors based on size()

i have a 2-d vector like vector < vector < coordinates > > v( points); where coordinates class is:

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

};

and points is 20. how to sort the individual vectors v[i] based on v[i].size() , ie based on number of coordinates objects pushed in v[i]. ???

like image 908
aseem Avatar asked Mar 04 '10 11:03

aseem


1 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);
like image 183
Will Avatar answered Sep 29 '22 14:09

Will