How to sort a multidimensional vector of floats?

后端 未结 3 1789
一向
一向 2021-01-23 14:59

So, I have a set of points in 3D, and I would like to store them in a 3 dimensional vector. Then I need sort that vector, giving priority first to the X dimention, then Y, then

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-23 15:17

    You could use an std::tuple to represent a point. The comparison for std::tuple works lexicographically, the way you want it to. Alternatively, you could provide a custom sort function to your vector of points. Something like this:

    sort(pointVector.begin(), pointVector.end(), [](const Point& lhs, const Point& rhs){//Implement your required comparison predicate here});
    

    Also, as this question shows, you can achieve some sort of a named-tuple-with-lexicographic-sorting by using std::tuples lexicographic sort and std::tie.

提交回复
热议问题