Sorting and finding index of a vector

vector<int> initial={4,7,16,18,8,11,10,15,5,9,2,6,17,19,12,13,1,3,14,20};
Vector<vector<int>> allparents;//This is a 2D vector of size [10][20] in which each row consists of a randomly shuffled vector of initial.

I want to first sort the initial vector in an ascending manner and generate a new vector (vector<int>ascending) and then find the corresponding index of that element in every row of the allparents vector.This is to repeated for all the rows of the allparents vector and then be stored in a new 2d vector(vector<vector<int>> index.I have written a function below which should do this,but it throws this error : Process returned -1073741819 (0xC0000005)
As i am new to vectors,I would request your help in debugging this error.Thanks.



void getindex(vector<vector<int>> allparents){
int ascending[20];
for(int i=0;i<20;i++){
ascending[i]=initial[i];
}
vector<vector<int>> index;
sort(ascending,ascending+20);
for(int k=0;k<10;k++){
for(int j=0;j<20;j++){
for(int z=0;z<20;z++){
if(ascending[j]==allparents[k][z]){
index[k][j]=z;
}
}
}

}
for(int q=0;q<10;q++){
for(int g=0;g<20;g++){
cout<<index[q][g]<<" ";
}
}
cout<<endl;
}

int main(){
getindex(allparents);
}
Last edited on
(1) PLEASE USE CODE TAGS

(2) Please consider the intelligibility of what you have written. I'm looking at:
I want to first sort the initial vector in an ascending manner [OK]
... and generate a new vector (vector<int>ascending) [OK]
... and then find the corresponding index of that element [WHAT ELEMENT?]
... of the allparents vector.
... This [WHAT?]
.... is to [BE] repeated for all the rows of the allparents vector [???]
.... and then [WHAT?] be stored in a new 2d vector(vector<vector<int>> index.


(3) As for your code, where are initial[] and index[][] defined?


And PLEASE DO NOT DOUBLE-POST.
Last edited on
Topic archived. No new replies allowed.