按多个条件对向量进行排序
Sort vectors by more conditions than one
我在其他 post 中询问了关于:my_old_post
但现在我需要更复杂的条件来对我的向量进行排序。
I have a vector like this: vector_points_original. Then if I sort it for z of each point I have other vector like: vector_points_sorted_by_Z. But I need vector_sorted_by_z and after sort first four points and second four points by y component. Could you help me?
它对性能至关重要吗?如果没有,只需将您现有的向量分成两个,按 Y 排序,然后将结果放回一个向量中 ?
std::vector<Point3d> sortedByZ;
std::vector<Point3d> firstFour(sortedByZ.begin(), sortedByZ.begin() + 4);
std::vector<Point3d> lastFour(sortedByZ.begin() + 5, sortedByZ.end());
// Sort firstFour and lastFour independently
sortedByZ = firstFour;
sortedbyZ.insert(sortedByZ.end(), lastFour.begin(), lastFour.end());
?
std::vector<CartesianPoint>v{...};//place your values here
//First, sort by Z
std::sort(v.begin(), v.end(), [](const auto& p1, const auto& p2){return p1.z < p2.z;});
//Define a compare-by-y lambda
auto yComp = [](const auto& p1, const auto& p2){return p1.y < p2.y;};
//Use yComp for first 4 v[]
std::sort(v.begin(), v.begin() + 4, yComp);
//And for the second
std::sort(v.begin() + 4, v.begin() + 8, yComp);
如果您在 y
重新排序时需要保存 z
订单,则 yComp = [](const auto& p1, const auto& p2) {return p1.y < p2.y && p1.z <= p2.z;};
我在其他 post 中询问了关于:my_old_post
但现在我需要更复杂的条件来对我的向量进行排序。
I have a vector like this: vector_points_original. Then if I sort it for z of each point I have other vector like: vector_points_sorted_by_Z. But I need vector_sorted_by_z and after sort first four points and second four points by y component. Could you help me?
它对性能至关重要吗?如果没有,只需将您现有的向量分成两个,按 Y 排序,然后将结果放回一个向量中 ?
std::vector<Point3d> sortedByZ;
std::vector<Point3d> firstFour(sortedByZ.begin(), sortedByZ.begin() + 4);
std::vector<Point3d> lastFour(sortedByZ.begin() + 5, sortedByZ.end());
// Sort firstFour and lastFour independently
sortedByZ = firstFour;
sortedbyZ.insert(sortedByZ.end(), lastFour.begin(), lastFour.end());
?
std::vector<CartesianPoint>v{...};//place your values here
//First, sort by Z
std::sort(v.begin(), v.end(), [](const auto& p1, const auto& p2){return p1.z < p2.z;});
//Define a compare-by-y lambda
auto yComp = [](const auto& p1, const auto& p2){return p1.y < p2.y;};
//Use yComp for first 4 v[]
std::sort(v.begin(), v.begin() + 4, yComp);
//And for the second
std::sort(v.begin() + 4, v.begin() + 8, yComp);
如果您在 y
重新排序时需要保存 z
订单,则 yComp = [](const auto& p1, const auto& p2) {return p1.y < p2.y && p1.z <= p2.z;};