I have two parallel arrays that I want to sort based on the contents of one of them:
void foo(vector<int>& a, vector<int>& b) {
assert(a.size() == b.size());
// I want elements in |b| to be sorted the same as elements in |a|.
sort(a.begin(), a.end()); // Not good: only sorts |a|.
}
Note, that it's trivial to do this with some extra vectors but I'd like to do this in-place. Again, I can probably hand roll some solution, but I was wondering if there's some standard library incantation that could take care of it ...
UPDATE
The suggested 'duplicate' How can I sort two vectors in the same way, with criteria that uses only one of the vectors? uses an extra index array to perform the sort. The whole point of the question is whether we can do that without allocating the extra array. Imagine if the vectors are really huge.