There is a rather typical task of sorting two arrays simultaneously, assuming that same indexed elements of the arrays form virtual pairs, which are sorted. Such questions appear at least 10 years ago: boost zip_iterator and std::sort
Now this task can be solved using range-v3 library:
#include <array>
#include <range/v3/all.hpp>
int main() {
auto x = std::array{ 3, 2, 4, 1 };
auto y = std::array{'A', 'B', 'C', 'D'};
ranges::sort( ranges::views::zip( x, y ) );
// here x = {1,2,3,4}, y={'D','B','A','C'}
}
Online demo: https://gcc.godbolt.org/z/WGo4vGsx5
In C++23 std::ranges::zip_view appears, and my expectation was that the same program can be written using the standard library only:
#include <array>
#include <ranges>
#include <algorithm>
int main() {
auto x = std::array{ 3, 2, 4, 1 };
auto y = std::array{'A', 'B', 'C', 'D'};
std::ranges::sort( std::views::zip( x, y ) );
}
Unfortunately, it results in long compilation errors. E.g. in GCC:
...
/opt/compiler-explorer/gcc-trunk-20221127/include/c++/13.0.0/bits/ranges_algo.h:54:31: error: no matching function for call to '__invoke(std::ranges::less&, std::pair<int, char>&, std::pair<int&, char&>)'
54 | return std::__invoke(__comp,
| ~~~~~~~~~~~~~^~~~~~~~
55 | std::__invoke(__proj, std::forward<_TL>(__lhs)),
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56 | std::__invoke(__proj, std::forward<_TR>(__rhs)));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
Online demo: https://gcc.godbolt.org/z/47xrzM6ch
Is it just because the implementations are not mature enough yet, or zip
view in C++23
will not help to sort two array?
zip_view
shouldn't use astd::pair
anymore (instead always astd::tuple
). This seems to not be implemented yet. Withstd::pair
I am not sure whether this was meant to work. Astd::pair
to references (iter_reference_t<I>
) is not comparable with astd::pair
to values (iter_value_t<I>
) without conversion, butstd::ranges::sort
requires thatiter_reference_t<I>
anditer_value_t<I>&
are comparable. – Radom