2

Suppose that I have two, for example, float arrays a and b, an int key array k and a template mySortByKey function of my own, operating on a single array, something like

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

Is there a possibility (for example, using zip iterators and tuples of some sorts) to enable mySort operating simultaneously on a and b, so that they can be simultaneously ordered according to the key k?

Vitality
  • 20,705
  • 4
  • 108
  • 146

1 Answers1

2

I don't think you can do that. However, you can accomplish something similar by the use of a helper array of indices.

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]];
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270