如何对浮点数的多维向量进行排序? [英] How to sort a multidimensional vector of floats?
问题描述
因此,我有一组3D点,我想将它们存储在3维向量中.然后,我需要对该向量进行排序,首先将X维度,Y,Z优先.例如,如果我有以下几点:
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 Z. So, for example, if I have this set of points:
P1 = (5, 10 ,9)
P2 = (1, 11, 4)
P3 = (8, 5, 2)
P4 = (5, 10, 3)
P5 = (5, 4, 0)
我想要一个像这样排序的向量:
I would like to get a vector sorted like this:
[1, 11, 4]
[5, 4, 0]
[5, 10, 3]
[5, 10, 9]
[8, 5, 2]
那么,如何考虑所有行对多维向量进行排序呢?
我应该改用std::priority_queue
吗?如果是这样,怎么显示我使用它?
So, how can a sort a multidimentional vector taking all rows into account?
Should I use std::priority_queue
instead? If so, how show I use it?
谢谢
推荐答案
...首先将X维度,Y,Z优先
...giving priority first to the X dimention, then Y, then Z
将 std::sort
与
Use std::sort
with std::tie
, something like following
#include <algorithm>
#include <tuple>
//....
struct Points // Your 3D Point
{
float x,y,z;
} ;
std::vector<Points> 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) ;
}
) ;
这篇关于如何对浮点数的多维向量进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!