Assume I have a vector<int>
intVec
and a vector<vector<double> >
matrix
. I want to sort intVec
and reorder the first dimension of matrix
correspondingly in C++. I realize this question has been asked several times before, but this case has a twist. A vector<double>
is expensive to copy, so e.g. copying both intVec
and matrix
to a vector<pair<int, vector<double> >
, sorting that and copying them back is even more inefficient than usual.
Short of rolling my own custom sorting algorithm, how can I sort intVec
and reorder the first dimension of matrix
in lockstep without copying any element of matrix
and invoking vector
's copy constructor?