使用std :: sort排序二维C数组 [英] Sort 2 dimensional c array with std::sort

查看:78
本文介绍了使用std :: sort排序二维C数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法使用std :: sort对二维c数组进行排序.但是,我可以对一维数组进行排序.这是在我将一个c数组交给一个c ++程序,希望进行排序而不将其复制到std :: array的情况下.也许有某种方法可以将其转换为std :: array而不进行复制?对于我来说,这听起来很令人怀疑,因为任何std :: array都会在它不拥有的内存上调用析构函数.

I cannot seem to sort a 2 dimensional c array with std::sort. I can however sort a one dimensional array. This is in a case where I am being handed a c array in a c++ program and am hoping to sort without copying it over to a std::array. Maybe there is some way to turn it into a std::array without copying it? That sounds doubtful to me as any std::array would then call a destructor on memory that it does not own.

排序一维c样式数组就可以了:

Sorting One dimensional c style array works just fine:

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样式数组进行排序无法编译:

Attempting to sort two dimensional c style array by second number does not compile:

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);

推荐答案

也许可以通过某种方式将其转换为 std :: array 而不进行复制它吗?

也许本身并没有变成 std :: array ,但是另一种方法可能是 cast 2D C样式的数组放入 std :: array reference 进行排序.依赖于标准的说法,即在内存中表示 std :: array 表示形式的方法至少应以等效于 C样式数组的方式开始.参见 [array.overview§2] :

Perhaps not turning into a std::array per se, but an alternative approach might be to cast the 2D C-style arrays into a std::array reference just for the sorting. Doing so in reliance on the standard saying an std::array representation in memory at least begins with its C-style array equivalent. See here under [array.overview§2]:

数组是一个聚合,最多可以使用N进行列表初始化类型可转换为T的元素.

An array is an aggregate that can be list-initialized with up to N elements whose types are convertible to T.

在实践中, reinterpret_cast 的以下用法很可能是安全的,但请注意,除非标准中某处有特殊例外,否则它的形式将是不确定的:

In practice, the following usage of reinterpret_cast is most probably safe, but do note that unless there is a special exception for it somewhere in the standard, it would formally be undefined behaviour:

#include <algorithm>
#include <array>
#include <iostream>

int main() {
  auto two_dim_less = [](std::array<int, 2>& a, std::array<int, 2>& b) {
      return a[1] < b[1]; };

  int two_dim[][2] = {{1, 8}, {2, 4}, {3, 10}, {4, 40}, {5, 1}};

  std::array<std::array<int, 2>, 5>& arr =
    *reinterpret_cast<std::array<std::array<int, 2>, 5>*>(&two_dim);

  std::sort(arr.begin(), arr.end(), two_dim_less);

  for (int i = 0; i < 5; i++)
    std::cout << two_dim[i][0] << ", " << two_dim[i][1] << '\n';

  return 0;
}

输出:

5, 1
2, 4
1, 8
3, 10
4, 40


关于 std :: qsort()的使用,请注意它可能比 std :: sort() 慢,因为后者允许内联比较,而前者则不允许内联.


Regarding the use of std::qsort(), note that it is potentially slower than std::sort() due to the latter allowing to inline the comparisons while the former doesn't.

这篇关于使用std :: sort排序二维C数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆