I have some data laid out as follows:
size_t num_elements = //...
some_type_t *data = //...
int *scores = //...
Each element data[i]
has a corresponding score in scores[i]
. I would like to sort both data
and scores
, using the array of scores
to order the data.
For example, for the data:
data = {'d', 'g', 'i', 'a', 'p'}
scores = {3, 5, 1, 2, 4}
the sorted version would be
data = {'i', 'a', 'd', 'p', 'g'}
scores = {1, 2, 3, 4, 5}
Is there a way to do this with the C++ standard library? I would prefer not to need to include Boost or libraries that have not yet been standardised.
I would also like to avoid unnecessarily copying the the data. This includes converting it to an array of structures.