C ++ works with packed arrays

Suppose I have two, for example, float arrays a and b , an array of keys int k and a template function mySortByKey mine, working in one array, something like

 template<class T> mySortByKey(int *k, T *a) 

Is it possible (for example, using zip iterators and incorrect tuples) to enable mySort while working on a and b at the same time, so that they can be ordered simultaneously according to the key k

+2
source share
1 answer

I do not think you can do this. However, you can achieve something similar with a helper index array.

 int keys[ARRAY_SIZE]; float a[ARRAY_SIZE]; float b[ARRAY_SIZE]; // Fill up the contents of keys, a, and b // Create an array of indices. int indices[ARRAY_SIZE]; for ( int i = 0; i < ARRAY_SIZE; ++i ) indices[i] = i; // Sort the indices using keys. mySortByKey(keys, indices); // Now access the arrays a and b indirectly, using the sorted array // of indices as an intermediate object. for ( int i = 0; i < ARRAY_SIZE; ++i ) { float fa = a[indices[i]]; float fb = b[indices[i]]; } 
+2
source

Source: https://habr.com/ru/post/949246/


All Articles