将向量排序的条件多于一个 [英] Sort vectors by more conditions than one

查看:119
本文介绍了将向量排序的条件多于一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他帖子中问过有关:

I asked in other post about: my_old_post

但是现在我需要更复杂的条件来对向量进行排序.

But now I need more complex condition to sort my vector.

我有一个像这样的向量: vector_points_original .然后,如果我对每个点的z进行排序,我就会有其他矢量,例如: vector_points_sorted_by_Z . 但是我需要 vector_sorted_by_z,然后按y分量对前四个点和后四个点进行排序.你能帮我吗?

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?

推荐答案

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;};

If you need to save z order while reordering by y, then yComp = [](const auto& p1, const auto& p2) {return p1.y < p2.y && p1.z <= p2.z;};

这篇关于将向量排序的条件多于一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆