Request Short Link
C++ 98
C++ 11
C++ 14
C++ 17
C++ 20
C++ 23
C++ 2c
for-loops as while-loops
array subscription
Show all implicit casts
Show all template parameters of a CallExpr
Use libc++
Transform std::initializer_list
Show noexcept internals
Show padding information
Show coroutine transformation
Show C++ to C transformation
Show object lifetime
Default
15
18
20
22
26
More
GitHub
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
None
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
#include <vector> #include <algorithm> #include <numeric> #include <cassert> using value_t = int; void swap_elements(size_t i, size_t j, std::vector<value_t>& v) { std::swap(v[i], v[j]); } template <class Vector, class... Vectors> void parallel_sort(Vector& keyvector, Vectors&... vectors) { std::vector<std::size_t> index(keyvector.size()); std::iota(index.begin(), index.end(), 0); std::sort(index.begin(), index.end(), [&](size_t a, size_t b) { return keyvector[a] < keyvector[b]; }); for (std::size_t i = 0; i < index.size(); i++) { if (index[i] != i) { (swap_elements(index[i], i, keyvector), ..., swap_elements(index[i], i, vectors)); std::swap(index[index[i]], index[i]); } } } void test(const std::vector<value_t>& vec, const std::vector<value_t>& res) { assert(vec == res); } value_t main() { std::vector<value_t> order{1, 0, 3, 2}; std::vector<value_t> v1{100, 200, 300, 400}; std::vector<value_t> v2{100, 200, 300, 400}; std::vector<value_t> v3{400, 200, 3000, 4000}; std::vector<value_t> v4{500, 200, 360, 400}; parallel_sort(order, v1, v2, v3, v4); test(v1, {200, 100, 400, 300}); test(v2, {200, 100, 400, 300}); test(v3, {200, 400, 4000, 3000}); test(v4, {200, 500, 400, 360}); }
Insight:
Console: