C++ sorting and keeping track of indexes

后端 未结 15 2293
甜味超标
甜味超标 2020-11-22 10:01

Using C++, and hopefully the standard library, I want to sort a sequence of samples in ascending order, but I also want to remember the original indexes of the new samples.<

15条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 10:24

    Suppose Given vector is

    A=[2,4,3]
    

    Create a new vector

    V=[0,1,2] // indicating positions
    

    Sort V and while sorting instead of comparing elements of V , compare corresponding elements of A

     //Assume A is a given vector with N elements
     vector V(N);
     int x=0;
     std::iota(V.begin(),V.end(),x++); //Initializing
     sort( V.begin(),V.end(), [&](int i,int j){return A[i]

提交回复
热议问题