A
A
AKurmazov2017-11-02 16:36:01
C++ / C#
AKurmazov, 2017-11-02 16:36:01

How to "pull" the elements of the second array while sorting the first one in c++?

How to sort the first array, pull the elements of the second one using the stl c++ sorting functions?
Example:
5 11 27 14 8 19 11
8 10 12 20 3 17 7
----------------------
5 8 11 11 14 19 27
8 3 10 7 20 17 12

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2017-11-02
@wataru

2 options:
1) make a new array with pair, write elements from two arrays there, sort.
2) create a new array, fill it with numbers from 0 to n-1. Pass your sorting function to sort, which compares not the passed numbers a and b, but the elements of the first array by these indices (array1[a] and array1[b]). Then use the resulting set of indices to display the second array (if you sorted the indices array, then output array2[indices[i]], for i from 0 to n-1).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question