简体   繁体   English

排序n维点并跟踪原始索引

[英]sort n-dimension point and keep track of original index

I have a set of n- dimension point store in vector< vector<double> > 我在vector< vector<double> >有一组n维点存储

ex A[0][1].............[N], and A[0][0] = X, A[0][1] = Y, A[0][2] = Z

and I want to sort the vector of all of the dimension 我想对所有维度的向量进行排序

ex sort X, Y, Z ,.........N in ascending order


ex              A[0] = (1,5,3), A[1] = (3,2,1) A[2] = (2,8,4) after sorting
index:            0               1             2
                A[0] = (1,5,3), A[1] = (2,8,4) A[2] = (3,2,1)
original index :  0               2             1

I find that sort(vector.begin(), vector.end()) can sort it but how can I record the original index with a additional vector? 我发现sort(vector.begin(), vector.end())可以对其进行排序,但是如何记录带有附加向量的原始索引?

Is there a algorithm or C++ feature can solve it? 是否有算法或C ++功能可以解决?

Thx in advance. 提前谢谢。

You need to somehow keep the information about the index. 您需要以某种方式保留有关索引的信息。 I can see two ways of doing this : 我可以看到两种方法:

1-Since you represent your point by a vector, you can had another dimension that will represent the orginal index : 1-由于您用矢量表示点,因此可以有另一个维来表示原始索引:

//Adding index info, inportant that your index be the last dimension , otherwise the sort is incorrect for(auto point = vector.begin(),unsigned int index=0;point != vector.end(); ++point,++index){ point->push_back(index) }; //添加索引信息,重要的是您的索引是最后一个维度,否则对于(auto point = vector.begin(),unsigned int index = 0; point!= vector.end(); ++ point, ++ index){point-> push_back(index)};

Then sort the same way you are doing now : 然后以与现在相同的方式进行排序:

sort(vector.begin(), vector.end()) sort(vector.begin(),vector.end())

and you access the original index with A[0][n] 并且您使用A [0] [n]访问原始索引

The cool thing about that is that it allow you to keep track of the index in a very convenient way, but you need to be able to modify the representation of your point. 酷的是,它允许您以非常方便的方式跟踪索引,但是您需要能够修改点的表示形式。

2- The other way is to have an external table of indices and sort that using custom comp. 2-另一种方法是拥有一个外部索引表,并使用自定义comp对其进行排序。 operator : 操作员:

you start by creating a vector of indices 您首先创建索引向量

std::vector indices(vector.size()); std :: vector索引(vector.size());

for(unsigned int index =0;index != indicies.size(); ++point){ indicies[index] = index ; for(unsigned int index = 0; index!= indicies.size(); ++ point){indicies [index] = index; }; };

//and sort... //并排序...

std::sort(indices.begin(),indices.end(),[&](unsigned int i,unsigned int j) { return vector[i] < vector[j]}) std :: sort(indices.begin(),indices.end(),[&](unsigned int i,unsigned int j){返回向量[i] <向量[j]})

now you need an additional level of indirection to go trough your points in the sorted order : A[indices[0]],A[indices[1]],... So the original position of A[indices[x]] is simply x 现在您需要一个额外的间接级别来按已排序的顺序处理点:A [indices [0]],A [indices [1]],...因此,A [indices [x]]的原始位置是简单地x

The main to remember between those two ways of doing it is that in the first you move your data around and not in the second , depending on what you are doing on your points, one might be better that the order 在这两种方法之间要记住的主要一点是,第一种方法是移动数据,而不是第二种,这取决于您在点上所做的事情,这可能比顺序更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM