按多个条件对向量进行排序
Sort vectors by more conditions than one
我在另一篇文章中问过:my_old_post
但是现在我需要更复杂的条件来对我的向量进行排序。
我有一个这样的向量:vector_points_original。 然后,如果我为每个点的 z 排序,我还有其他向量,例如:vector_points_sorted_by_Z。 但是我需要vector_sorted_by_z,然后按 y 分量对前四点和后四点进行排序。你能帮我吗?
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;};
它对性能至关重要吗?如果没有,只需将现有向量一分为二,按 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<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- 如何从C++的对的排序向量中获取有关给定值的相应对
- 对象接收堆栈溢出异常 c++ 的排序向量
- "x"的所有元素都存在于"y"(排序向量)中吗?
- 指向指针排序向量的指针向量的 C++ 向量
- 未排序向量上的lower_bound/upper_bound
- 智能指针的排序向量:神秘崩溃
- 按类型排序向量并按类型或派生类型搜索
- 如何在不使用标准算法的情况下在排序向量中添加 c 元素?
- C++排序向量<double>与在<Object>双成员变量上键控的向量
- 字符串指针的排序向量
- 具有前导数字的字符串的排序向量
- 保证重新排序向量
- 结构解释的C 排序向量
- 更新排序向量的一个条目
- 通过在排序向量上使用二叉搜索来定位未排序向量中的元素
- 排序向量上 std::find_if 和 std::bind2nd 的替代品
- 使用QuickSort算法以降序排序向量