vendredi 1 juillet 2016

Sorting a dynamically allocated, multidimensional C array with std::sort and lambda function in C++11

I'm trying to sort a 2D, dynamically allocated array using std::sort with a custom sorting function using lambda. numFaces is an integer value determined at the start of the program and doesn't change during the program's lifespan. Here's my current approach.

float(*data)[24] = new float[numFaces][24];
std::sort(data, data + numFaces,  [](float (&A)[24], float (&B)[24]) -> bool
{
    return comparison(A, B); // Pseudo
});

The program is failing to compile with this error:

array type 'float [24]' is not assignable

Since I've specified in the lambda declaration that the parameters should be references I can't see why the compiler is producing this error message. I am using the VC++ compiler from Microsoft's Visual Studio Community Edition 2015. Here's a quick pastebin of the entire log

Line 38 is the closing bracket of the lambda function declaration.

I know I could solve this in a couple different ways, but if there is a way to make this work, I would prefer to continue like this. If you have a suggestion to another solution that lets the data be stored contiguously and sorted in these groups of 28 floats I would be delighted to hear about that too.

Ways I could solve the current issue which would introduce other issues and/or bigger delay in the application:

  • Using qsort with void pointers, casting them and sorting pretty much the same way. I'm a bit unsure whether or not this would introduce any more delay when the std::sort doesn't have all the information about the container that it would have if I was using std::vectors.
  • Using std::sort with nested std::vectors. The data wouldn't always be stored contiguously on memory, which would in turn force me to create copies of the data every time the vector is sorted. I tested this and checked the locations on memory with the VS debugger, but again I am not 100% sure this can't be solved somehow.
  • Using std::vector of a custom class/struct with the needed data. If there is no simple solution to my problem I will do this or do the sorting without any STL calls.

Tiny notice: The code in the code tag above has been stripped of unnecessary code. The pastebin is a bit different because of this.

Aucun commentaire:

Enregistrer un commentaire