如何对浮点数的多维向量进行排序?
How to sort a multidimensional vector of floats?
所以,我有一组 3D 点,我想将它们存储在一个 3 维向量中。然后我需要对该向量进行排序,首先优先考虑 X 维度,然后是 Y,然后是 Z。因此,例如,如果我有这组点:
P1 = (5, 10 ,9)
P2 = (1, 11, 4)
P3 = (8, 5, 2)
P4 = (5, 10, 3)
P5 = (5, 4, 0)
我想要一个这样排序的向量:
[1, 11, 4]
[5, 4, 0]
[5, 10, 3]
[5, 10, 9]
[8, 5, 2]
那么,如何对考虑到所有行的多维向量进行排序?
我应该改用 std::priority_queue
吗?如果是这样,我如何使用它?
谢谢
您可以按字典顺序使用 std::tuple<double, double, double>
to represent a point. The comparison for std::tuple
作品,以您希望的方式使用。或者,您可以为点向量提供自定义排序功能。像这样:
sort(pointVector.begin(), pointVector.end(), [](const Point& lhs, const Point& rhs){//Implement your required comparison predicate here});
此外,作为 this question shows, you can achieve some sort of a named-tuple-with-lexicographic-sorting by using std::tuple
s lexicographic sort and std::tie
。
...giving priority first to the X dimention, then Y, then Z
#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) ;
}
) ;
您可以使用 std::sort()
通过制作自己的 comparator 函数轻松地根据您的特定条件进行排序。
假设您已将单个 3D 点存储在 struct point
中,并且 点 存储在 std::vector<points>
中(A std::tuple
可能是更有用。),试试这个代码。
示例:
#include <vector>
#include <algorithm>
using namespace std;
struct point
{
float x, y, z;
}
bool mySort(const point& a, const point& b)
{
//A naive comparison to help you understand better.
//You could always use std::tie for lexicographical comparison.
if (a.x == b.x)
{
if (a.y == b.y)
return a.z < b.z;
else
return a.y < b.y;
}
else
return a.x < b.x;
}
int main()
{
vector<point> graph;
//push_back() all your points into the graph.
//mySort() is a custom comparator function.
sort(graph.begin(),graph.end(),mySort);
}
所以,我有一组 3D 点,我想将它们存储在一个 3 维向量中。然后我需要对该向量进行排序,首先优先考虑 X 维度,然后是 Y,然后是 Z。因此,例如,如果我有这组点:
P1 = (5, 10 ,9)
P2 = (1, 11, 4)
P3 = (8, 5, 2)
P4 = (5, 10, 3)
P5 = (5, 4, 0)
我想要一个这样排序的向量:
[1, 11, 4]
[5, 4, 0]
[5, 10, 3]
[5, 10, 9]
[8, 5, 2]
那么,如何对考虑到所有行的多维向量进行排序?
我应该改用 std::priority_queue
吗?如果是这样,我如何使用它?
谢谢
您可以按字典顺序使用 std::tuple<double, double, double>
to represent a point. The comparison for std::tuple
作品,以您希望的方式使用。或者,您可以为点向量提供自定义排序功能。像这样:
sort(pointVector.begin(), pointVector.end(), [](const Point& lhs, const Point& rhs){//Implement your required comparison predicate here});
此外,作为 this question shows, you can achieve some sort of a named-tuple-with-lexicographic-sorting by using std::tuple
s lexicographic sort and std::tie
。
...giving priority first to the X dimention, then Y, then Z
#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) ;
}
) ;
您可以使用 std::sort()
通过制作自己的 comparator 函数轻松地根据您的特定条件进行排序。
假设您已将单个 3D 点存储在 struct point
中,并且 点 存储在 std::vector<points>
中(A std::tuple
可能是更有用。),试试这个代码。
示例:
#include <vector>
#include <algorithm>
using namespace std;
struct point
{
float x, y, z;
}
bool mySort(const point& a, const point& b)
{
//A naive comparison to help you understand better.
//You could always use std::tie for lexicographical comparison.
if (a.x == b.x)
{
if (a.y == b.y)
return a.z < b.z;
else
return a.y < b.y;
}
else
return a.x < b.x;
}
int main()
{
vector<point> graph;
//push_back() all your points into the graph.
//mySort() is a custom comparator function.
sort(graph.begin(),graph.end(),mySort);
}