Suppose I have a random access iterator (see also relevant type traits) representing keys, and a random access iterator representing values (not necessarily a single value, mind you!). Is it possible to combine them into a new random access iterator to perform, say, simultaneous sort with std::sort
(by keys, but permute both keys and values at the same time)? std::sort
only accepts one iterator. And how do I do it using only core C++?
I tried to specify a new iterator over a tuple of references, but the problem is how do I make swap work with rvalues (with some proxies?)?
Moreover, these two sequences are of huge size, so I cannot copy their values to create pairs, or even allocate a data structure of references. In fact, you can try and see that you cannot sort a vector of reference pairs (think about the behavior of operator=
in this context). So, this code will fail (unless std::sort
decides to use swap
only):
int arr1[] = {3, 2, -100, 5, 6, -200, 4, 0, -1, 2, 11, 12, -3, -4, -15};
int arr2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
std::vector<std::tuple<int&, int&>> pairs;
for (int i = 0; i < len; ++i) {
pairs.push_back(std::tie(arr1[i], arr2[i]));
}
std::sort(pairs.begin(), pairs.end(), [](auto lhs, auto rhs) {return std::get<0>(lhs) < std::get<1>(rhs);});
arrays print outs:
-15 -15 -15 -15 -15 -15 -15 -15 -15 -15 -15 -15 -15 -15 -15
14 14 14 14 14 14 14 14 14 14 14 14 14 14 14
So far I found this: https://web.archive.org/web/20120422174751/http://www.stanford.edu/~dgleich/notebook/2006/03/sorting_two_arrays_simultaneou.html. This solution uses boost, however, and is not swap compatible.
Update: I was able to resolve the swap problem by creating a wrapper over a tuple of references. But now I have a move problem. std::sort
extracts and assigns values. But how do I do that for references? (see the broken code above).