我想对一个结构数组进行排序,其中每个结构都有一个数组。我想创建我的自定义compare()函数进行排序但尚未能够:
struct box{
int dims[6];
} boxArray[32];
所以,有32个盒子,每个盒子有6个尺寸。
例如,
box1具有以下尺寸:
1 2 5 10 20 30
BOX2:
1 2 3 4 5 6 9
我希望box2在box1之前排序,因为在前两个维度中它们相等但在下一个维度中,框2更小。
我的想法是使用sort
(boxArray,boxArray + 32,customCmpBoxes)
并在自定义函数内部,递归比较,直到一个较小(或不是),但我无法使其正常工作。
答案 0 :(得分:1)
bool customCmpBoxes(const box& left, const box& right) {
return std::lexicographical_compare(
left.dims, left.dims + sizeof(left.dims)/sizeof(left.dims[0]),
right.dims, right.dims + sizeof(right.dims)/sizeof(right.dims[0]));
}