我似乎无法使用std::sort对一个二维c数组进行排序。但是我可以对一个一维数组进行排序。这种情况是在我在一个c++程序中被传递一个c数组,并希望在不复制到std::array的情况下进行排序。也许有一种方法可以将其转换为std::array而不需要复制它吗?但这对我来说听起来很可疑,因为任何std::array都会调用它不拥有的内存上的析构函数。
对一维c风格数组进行排序完全没有问题:
int len = 5;
auto one_dim_less = [](int a, int b){
return a < b;
};
int one_dim[] = {4, 0, 3, 1, 2};
std::sort(one_dim, one_dim + len, one_dim_less);
尝试按第二个数字对二维C风格数组进行排序会导致编译错误:
int len = 5;
auto two_dim_less = [](int a[2], int b[2]){
return a[1] < b[1];
};
int two_dim[][2] = {{1,8}, {2,4}, {3,10}, {4,40}, {5,1}};
std::sort(two_dim, two_dim + len, two_dim_less);
two_dim[0] = two_dim[1]
是无效的。 - Holt