【发布时间】:2023-03-10 19:11:02
【问题描述】:
我有一个std::vector<QVector3D>,其中包含一些 3D 坐标。我想按z 值对vector 进行排序。
我将四个 3D 点循环推入向量中:
/* points
29.3116 -192.771 -103.172
2.50764 -190.652 -194.383
24.1295 -181.255 -179.553
6.22275 -176.747 -189.578
*/
// Find the points and push in vector
...
std::vector<QVector3D> pointVector;
pointVector.push_back(QVector3D(point[0], point[1], point[2]));
// iterate through vector
for(int i= 0; i< pointVector.size(); i++)
{
qDebug()<<"Vector: " << pointVector[i].x() << pointVector[i].y() << pointVector[i].z();
}
如果我按照z 坐标对vector 进行排序,输出应该是这样的:
2.50764 -190.652 -194.383
6.22275 -176.747 -189.578
24.1295 -181.255 -179.553
29.3116 -192.771 -103.172
std::sort
【问题讨论】:
-
这是否通过重载std::sort
https://stackoverflow.com/questions/20039912/how-can-i-overload-a-custom-stdsort-comparison-function987654328@的比较函数来解决你的问题
标签: c++