Here's my structure:
struct animal
{
int position;
int** shape;
int rows, cols;
int force;
int minSafety;
void init(int r,int c,int k, int t, int p)
{
shape = new int*[r];
for(int i = 0; i < r; i++)
{
shape[i] = new int[c];
}
rows = r;
cols = c;
force = k;
minSafety = t;
position = p;
}
~animal()
{
for(int i = 0; i < rows; i++)
{
delete[] shape[i];
}
delete[] shape;
}
};
I have an array of such structures and I want to sort that array in ascending order by the "force". Here's my array and the predicate function I use to pass to the "sort" function from the STL.
bool sortByForce(animal& animal1, animal& animal2)
{
return animal1.force != animal2.force ?
animal1.force < animal2.force : animal1.rows * animal1.cols > animal2.rows * animal2.cols;
}
animal* animals = new animal[P];
//sort(animals, animals+P, &sortByForce);
When I uncomment the sort function the code breaks. I think it's because of the dynamic arrays inside the structures. (It actually sorts the array but the "shape" arrays are broken iside the structures.) Thanks :)