I have two arrays (same size) and I need to sort them with only one call to std::sort, without any extra memory allocations and/or copying.
We have as input:
int *array_1;
int *array_2;
int size;
I have searched a lot and I have found so far this should be possible (given how flexible std is) if I use a custom iterator (and maybe a custom comparison function too). Can someone please provide infomation on how I can do this with std::sort?
For example if the arrays are:
array_1 = {15, 7, 3, 5} and array_2 = {1, 2, 6, 21}
I want them to be:
array_1 = {3, 5, 7, 15} and array_2 = {6, 21, 2, 1}
Here is the non working code I am trying to fix:
struct double_iterator
{
T* const a; U* const b; size_t i;
struct ref {
T& p; U& q;
ref(T& p, U& q) : p(p), q(q) {}
};
ref operator*() { return ref(a[i], b[i]); }
inline void operator=(ref x, ref y) { x.p = y.p; x.q = y.q; }
inline bool operator<(ref x, ref y) { return x.p < y.p; }
};
std::sort(double_iterator(array_1,array_2,0), double_iterator(array_1,array_2,array_size));
Aucun commentaire:
Enregistrer un commentaire