How to sort a multidimensional vector of floats?

后端 未结 3 1744
一向
一向 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:18

    ...giving priority first to the X dimention, then Y, then Z

    Use std::sort with std::tie, something like following

    #include 
    #include 
    //....
    
    struct Points // Your 3D Point
    {
        float x,y,z;
    } ;
    
    std::vector v; // Vector of 3D points
    
    std::sort( v.begin(), v.end(), 
                []( const Points& lhs, const Points& rhs )
                {
                    return std::tie(lhs.x,lhs.y,lhs.z) 
                         < std::tie(rhs.x,rhs.y,rhs.z)  ; 
    
                }
             ) ;
    

    DEMO

提交回复
热议问题