2017-03-17 39 views
0

我问其他职位有关:my_old_post排序向量由多个条件比一个

但现在我需要更复杂的条件来排序我的向量。

我有一个这样的矢量:vector_points_original。然后,如果我为每个点的z排序,我有其他矢量,如:vector_points_sorted_by_Z但我需要 vector_sorted_by_z并按y分量排序第一个四点和第二个四点之后。你可以帮帮我吗?

+0

请在这里发表的文字,而不是发布一个链接到文本的图像。 –

回答

0
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); 

如果您需要保存z秩序,同时通过y重新排序,然后yComp = [](const auto& p1, const auto& p2) {return p1.y < p2.y && p1.z <= p2.z;};

0

性能至关重要吗?如果没有,只需将现有的矢量分成两部分,按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()); 

+0

感谢所有,它工作正常。 –