开发者

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;
    }

};

a开发者_如何学Pythonnd 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]. ???


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);


  1. make a function which you can use whatever attributes to compare the objects and then use STL sort() algorithm to sort the container.

  2. overload the < operation of that class and make it the same as the above function. Then you can use the sort() function provided by the STL containers (like STL list).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜