0

I am looking for a way to do the following:

std::vector<char> v1{'a', 'z', 'b', 'c'};
std::vector<int> v2{1, 2, 3, 4};
std::special_sort(v1, v2);

// Output:
// v1: a, b, c, z
// v2: 1, 3, 4, 2

So v1 should be aligned by the result of a sort on v1.

Is there a function in the standard that does special_sort?

What is such a sort called?

User12547645
  • 6,955
  • 3
  • 38
  • 69
  • Cannot you use a single vector where each element is the pair? E.g., `std::vector> v{{'a', 1}, {'z', 2}. ...}`. In such a way the solution is pretty straightforward – BiagioF Apr 21 '20 at 18:52
  • Thought about that as well, but its not really an option – User12547645 Apr 21 '20 at 18:53
  • 1
    You can use a predicate that refers to v1 and v2 by index, and pass in a `vector` that represents the index position (pre-initialized from 0 ... size-1). I assume v1 and v2 are same size. Then the sorted index position can be used to construct a new v1 and new v2 from the old ones. – Eljay Apr 21 '20 at 18:55

0 Answers0