Re: Sorting parallel arrays in C++
Quote:
Originally Posted by
GCDEF
I hope the point of the exercise was to demonstrate what a pain parallel arrays are and how buggy they can be. If he's suggesting you write code that way, you need a new instructor.
There are cases where it's desired to do it this way. Particularly if you need locality of data. Splitting an array of structures up into multiple arrays of smaller elements/structures can sometimes cause a huge performance boost if it means you can get more data into the L1/L2 CPU cache. So it isn't an entirely pointless exercise unlike some others we've seen :-p.
But yes, it is a pain, it is error prone, and it means you can't use sorting libraries/templates unless they have a means to override/overload/templatize the element swap (and don't optimize it away in cases).
std::sort may not Always work with a custom swap() depending on implementation (small ranges can be sorted with an insertion sort which uses range swaps via move_backward. I'm not sure if c++11 made any guarantee changes on this...
so unless you relaly really have to, it's better to sort a single array of structures rather than trying to sort many arrays and keep them in sync.
Re: Sorting parallel arrays in C++
Quote:
Originally Posted by
OReubens
...it means you can't use sorting libraries/templates unless they have a means to override/overload/templatize the element swap (and don't optimize it away in cases).
You could always call the algorithm with a pair of boost::zip_iterators. You'll need to write a custom (or generic reusable) comparator though, which is not so nice without having support for generic lambdas (should come with C++14).