【问题标题】:c++ 3d array function returnsc++ 3d数组函数返回
【发布时间】:2021-12-22 00:57:41
【问题描述】:

所以我得到了这个看起来像这样的 3 维数组:

int map[100][100][100];

我想将它用作函数的返回类型,无论是作为指针还是类似的东西:

int* World::CreateCell() {

    int map[100][100][100];

    return map;

}

但是我找不到适合 3d 数组的返回类型,它不会让我像 2D 数组那样使用 int*。

即使这样的事情也行不通:

int a[100][100][100];
int* b = a;

VS 似乎认为数据类型是int*(100)(100),但这没有意义,也不起作用。

对于它的价值,我已经用谷歌搜索了这个并没有看到任何解决方案。谢谢

【问题讨论】:

  • 你走错了路。返回指向本地数组的指针对于二维数组也是错误的。改用嵌套的std::arraystd::vector
  • 在 C++ 中,您通常不应该使用 C 样式的数组。没有理由这样做。如果您遵循此规则,那么大多数此类问题都会消失。 . .
  • 我得到了你的兄弟,用嵌套向量对其进行排序
  • 仅作记录:Demo on coliru.

标签: c++ arrays multidimensional-array return-type


【解决方案1】:

由于您需要 3D C 样式的数组,因此您需要有一个指向指针的指针,即 int***。此外,如果您使用这样的创建函数,则需要分配内存。否则,您从该函数返回静态分配的内存。

下面是一个简单的例子:

#include <iostream>

static int*** create_cell() {    
    constexpr std::size_t n = 100;
    int*** map = new int**[n];
    for (std::size_t i = 0u; i < n; ++i) {
        map[i] = new int*[n];
        for (std::size_t j = 0u; j < n; ++j) {    
            map[i][j] = new int[n];
        }
    }   
    return map;
}

static void delete_cell(int***& map) { 
    constexpr std::size_t n = 100;
    for (std::size_t i = 0u; i < n; ++i) {
        for (std::size_t j = 0u; j < n; ++j) {    
            delete[] map[i][j];
        }
        delete[] map[i];
    }
    delete[] map;    
}

int main()
{
    int*** a = create_cell();

    a[0][0][0] = 1;
    std::cout << "a[0][0][0] = " << a[0][0][0] << std::endl;
    
    delete_cell(a);

    return 0;
}

这取决于您的用例:但是对于现代 c++,您可以使用来自 stl 的容器(例如 std::vectorstd::array)来简化您的生活。在此处查看以供参考:std::arraystd::vector

例如,您可以定义您的 3D 类型:

#include <array>
#include <vector>
using vector_3d = std::vector<std::vector<std::vector<int>>>;
using array_3d = std::array<std::array<std::array<int, 100>, 100>, 100>;

然后将它们用作:

array_3d b;
b[0][0][0] = 1;
std::cout << "b[0][0][0] = " << b[0][0][0] << std::endl;

【讨论】:

    【解决方案2】:

    首先你应该

    永远不要返回指向本地非静态变量的引用或指针。

    现在来回答你的问题:

    我想将它用作函数的返回类型,无论是作为指针还是类似的东西。但是我找不到适合 3d 数组的返回类型。

    这(下)是如何为 3D 数组执行此操作的。基本上有2种方法来解决这个问题:

    方法一

    //CreateCell is a function that returns a pointer to a 3D array of the size 100,100,100
    int (*CreateCell())[100][100][100] {
    
        int map[100][100][100];
    
        return &map;
    }
    

    方法 1 可以看到here

    方法二

    //CreateCell is a function that returns a pointer to a 3D array of the size 100,100,100
    auto CreateCell() -> int(*)[100][100][100] {
    
        int map[100][100][100];
    
        return &map;
    }
    

    方法 2 使用 尾随返回类型,如here 所示工作。

    注意

    这两种方法都返回一个指向必须避免的局部变量的指针。我已经给出/编写了答案,以便您可以看到如何根据需要返回 3D 数组的指针。您可以改为按值创建并返回 3D `std::vector`。

    【讨论】:

      【解决方案3】:

      考虑在矩阵周围使用简单的包装器:

      struct Wrapper { int Mat[100][100][100] = {0}; };
      

      然后签名会变成这样:

      Wrapper *foo(...) { ... }
      

      这是一个简单的工作示例:

      #include <iostream>
      struct Wrapper { int Mat[100][100][100] = {0}; };
      Wrapper *inc(Wrapper *w)
      {
          for (int i=0;i<100;i++)
              for (int j=0;j<100;j++)
                  for (int k=0;k<100;k++)
                      w->Mat[i][j][k] += (i+j+k);
          return w;
      }
      int main(int argc, char **argv)
      {
          Wrapper w;
          Wrapper *out = inc(&w);
          std::cout << out->Mat[5][6][7] << "\n";
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-04
        • 2012-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-31
        • 2012-04-01
        相关资源
        最近更新 更多