sort n-dimension point and keep track of original index

I have a set of n- dimension point store in `vector< vector<double> >`

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?

Is there a algorithm or C++ feature can solve it?

Thx in advance.

I have tried to solve it with a class wrapper it but I don't know how to write the compare function.
class point{
    point(int totalLength = 0, int elementLength = 0);
    vector<vector<double> > pointSet;//store the n-D points
    vector<double> pointIndex;//store the index
point::point(int totalLength, int elementLength){
    pointSet.resize(totalLength,vector<double>(elementLength, 0));
Last edited on
> but how can I record the original index with a additional vector?

You can copy all the elements of the original vector to a new one, then sort the original
Sorry,it is my bad, I want to track the original index after sorting.
create a data stucture which contains a vector 3d and an integer.

Now your main vector mycontainer will contain a bunch of objects of the same type as you created above. As you fill the vector, the integer part of the data structure can be updated as well. When you sort, you still have the same index stored at each position by accessing mycontainer[i].integer and access each cordinate with mycontainer[i].3d

You can also store them as pairs with pair.first = 3d and pair.second = integer.
Last edited on
Topic archived. No new replies allowed.