C++ 如何对多维浮点向量进行排序?
所以,我有一组三维点,我想把它们存储在一个三维向量中。然后我需要对向量进行排序,优先考虑X维,Y维,Z维。例如,如果我有这组点:C++ 如何对多维浮点向量进行排序?,c++,sorting,vector,priority-queue,C++,Sorting,Vector,Priority Queue,所以,我有一组三维点,我想把它们存储在一个三维向量中。然后我需要对向量进行排序,优先考虑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] sort(pointVector.begin(), pointVector
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]
sort(pointVector.begin(), pointVector.end(), [](const Point& lhs, const Point& rhs){//Implement your required comparison predicate here});
那么,如何在考虑所有行的情况下对多维向量进行排序呢?
我应该改用std::priority\u queue
吗?如果是,我如何使用它
谢谢您可以使用表示点的。这本书是按你所希望的方式按词典编纂的。或者,您可以为点向量提供自定义排序函数。大概是这样的:
[1, 11, 4]
[5, 4, 0]
[5, 10, 3]
[5, 10, 9]
[8, 5, 2]
sort(pointVector.begin(), pointVector.end(), [](const Point& lhs, const Point& rhs){//Implement your required comparison predicate here});
另外,如图所示,您可以通过使用std::tuple
s字典排序实现某种命名元组的字典排序,并且可以使用来表示点。这本书是按你所希望的方式按词典编纂的。或者,您可以为点向量提供自定义排序函数。大概是这样的:
[1, 11, 4]
[5, 4, 0]
[5, 10, 3]
[5, 10, 9]
[8, 5, 2]
sort(pointVector.begin(), pointVector.end(), [](const Point& lhs, const Point& rhs){//Implement your required comparison predicate here});
此外,如图所示,通过使用std::tuple
s字典排序和,您可以实现某种具有字典排序的命名元组
…优先考虑X维度,然后是Y,然后是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) ;
}
) ;
#包括
…优先考虑X维度,然后是Y,然后是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) ;
}
) ;
#include您可以使用std::sort()
通过自己的比较器功能,根据您的具体情况轻松排序
假设在struct point
中存储了单个3D点,并且在std::vector
中存储了点(astd::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);
}
#包括
#包括
使用名称空间std;
结构点
{
浮动x,y,z;
}
布尔·迈索尔(康斯特点a、康斯特点b)
{
//一个天真的比较,帮助你更好地理解。
//您可以始终使用std::tie进行词典比较。
如果(a.x==b.x)
{
如果(a.y==b.y)
返回a.z
您可以使用std::sort()
通过自己的比较器功能,根据您的具体情况轻松排序
假设在struct point
中存储了单个3D点,并且在std::vector
中存储了点(astd::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);
}
#包括
#包括
使用名称空间std;
结构点
{
浮动x,y,z;
}
布尔·迈索尔(康斯特点a、康斯特点b)
{
//一个天真的比较,帮助你更好地理解。
//您可以始终使用std::tie进行词典比较。
如果(a.x==b.x)
{
如果(a.y==b.y)
返回a.z
您可以将std::sort与自定义的比较函数一起使用。您能稍微扩展一下吗。。。因此,基本上,没有一个标准的方法来做到这一点。我必须拿出我自己的函数…?你可以使用std::sort和一个自定义的比较函数。你能扩展一下吗。。。因此,基本上,没有一个标准的方法来做到这一点。我必须拿出我自己的功能。。。?