Sorting a vector of custom objects

后端 未结 13 2929
既然无缘
既然无缘 2020-11-21 05:14

How does one go about sorting a vector containing custom (i.e. user defined) objects.
Probably, standard STL algorithm sort along with a predicate (a fu

13条回答
  •  不知归路
    2020-11-21 05:30

    To sort a vector you can use the sort() algorithm in .

    sort(vec.begin(),vec.end(),less());
    

    The third parameter used can be greater or less or any function or object can also be used. However the default operator is < if you leave third parameter empty.

    // using function as comp
    std::sort (myvector.begin()+4, myvector.end(), myfunction);
    bool myfunction (int i,int j) { return (i

提交回复
热议问题