如何在 C++ 中发送二维数组作为参考并返回?

发布于 2024-12-06 08:34:29 字数 545 浏览 0 评论 0原文

另一个初级到中级的问题。我正在尝试将二维数组传递给 C++ 中的函数。我知道数组不能直接发送到函数,所以我首先创建了一个指针(名称已编辑,但你会明白的):

double input[a][b] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
Class.calculate (*(input), a, b);

然后我尝试在所述函数中使用它 - 但看起来我是不知道如何取消引用指针以便能够再次处理二维数组。我的代码(或多或少):

for (int x=0; x<=a; x++){
    for (int y=0; y<=b; y++){
        tmpInput[x][y]= (*input)[x][y];
    }
}

编译器抱怨一个错误,即数组下标的无效类型'double [int]',但我仍然无法弄清楚问题。我最好的选择是我没有正确取消引用二维数组,但另一个选择是 C++ 不能直接取消引用二维数组,而是依赖在发送数组之前将其转换为一维数组。有什么想法吗?

yet another beginner-to-intermediate question. I'm trying to pass a 2-D array to a function in C++. I'm aware that the array can't be sent directly to the function, so I first created a pointer (names edited but you'll get the idea):

double input[a][b] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
Class.calculate (*(input), a, b);

Then I try to use it in said function - but seemingly I'm unaware on how to dereference the pointer to be able to handle the 2-D array again. My code (more or less):

for (int x=0; x<=a; x++){
    for (int y=0; y<=b; y++){
        tmpInput[x][y]= (*input)[x][y];
    }
}

The compiler complains about an error, namely invalid types ‘double[int]’ for array subscript, but I still can't figure out the problem. My best bet is that I didn't dereference the 2-D array properly, but the other option is that C++ can't dereference 2-D arrays directly, instead relying in converting the array to 1-D before sending it. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏末染殇 2024-12-13 08:34:29

为什么数组不能直接发送给函数?

Call Class.calculate (*(input), a, b); 正在尝试取消引用 input ,但您无法执行此操作,因为它不是指针。

如果calculate定义为:

Class::calculate(double *input[], size a, size b)

您只需调用Class.calculate(input, a, b)即可。

Why the array cannot be sent directly to a function?

Call Class.calculate (*(input), a, b); is trying to dereference input which you can't do as it is not a pointer.

If the calculate is defined as:

Class::calculate(double *input[], size a, size b)

You can just call Class.calculate(input, a, b).

动次打次papapa 2024-12-13 08:34:29

我知道数组不能直接发送到函数,所以我首先创建了一个指针(名称已编辑,但您会明白的)

错了。你可以通过它。只需将calculate 成员函数的第一个参数设置为指向指针的指针即可。

calculate( double **ptr, int rows, int columns ){

   // Now access the array via ptr as usual with the [] operator. 
   // like ptr[0][1]
}

并像这样称呼它 - Class.calculate(input, a, b);

I'm aware that the array can't be sent directly to the function, so I first created a pointer (names edited but you'll get the idea)

Wrong. You can pass it. Just have the calculate member function's first argument to be pointer to a pointer.

calculate( double **ptr, int rows, int columns ){

   // Now access the array via ptr as usual with the [] operator. 
   // like ptr[0][1]
}

And call it like - Class.calculate (input, a, b);

旧时浪漫 2024-12-13 08:34:29

还没人说起模板吗?我很失望!


实际上,您可以将其作为真实参考传递,而无需在 C++ 中将大小作为额外参数传递:

class Class{
public:
  // ...
  template<unsigned N, unsigned M>
  void calculate(double (&arr)[N][M]){
    // use it like normal, arr[x][y]
    // ...
  }
  // ...
};

Ideone 上的示例。

尽管这没有回答您提出的确切问题,但了解这些东西总是好的。 :) 不管您是初学者、中级还是专业人士,模板毕竟是 C++ 的重要组成部分。从第一天起,你就会使用它们,尽管你可能并不知情。

No one said anything about templates yet? I'm disappointed!


You can actually pass it as a real reference without the need to pass the size as extra parameters in C++:

class Class{
public:
  // ...
  template<unsigned N, unsigned M>
  void calculate(double (&arr)[N][M]){
    // use it like normal, arr[x][y]
    // ...
  }
  // ...
};

Example on Ideone.

Even though this doesn't answer the exact question you asked, it's always good to know such stuff. :) Templates are an important part of C++ after all, no matter if you're beginner, intermediate or a pro. You use them, though maybe not knowingly, from day 1.

メ斷腸人バ 2024-12-13 08:34:29

如果没有看到 calculate() 的声明,很难确定,但我猜这是参数类型不匹配。您可以将数组作为 double* 发送,但不能作为 double** 发送 - 它一个双精度数组(它们被放置在在内存中线性输出,即使编译器允许您将它们视为多维数组)。但它不是一个指向双精度的指针数组,这就是double**类型的实际含义:取消引用double**并且你得到一个double*

如果您知道第二个下标是常量(在您的情况下为 2),则可以定义calculate() 来期望 N*2 双精度数组:

void calculate(double array[][2], int a)
{
    //Whatever
}

或者,如果两个维度都可以更改,则只需采用直接 double* 并自己进行指针数学运算(调用此函数时,您可能必须将数组转换为 double* ,但它会起作用):

void calculate(double* array, int a, int b)
{
    //To get array[i][j]:
    array[i * b + j];
}

希望这有帮助!

It's hard to say for sure without seeing the declaration of calculate(), but I'd guess it's an argument type mismatch. You can send your array as a double*, but not as a double** - it is an array of doubles (they're laid out linearly in memory, even if the compiler lets you treat them as a multidimensional array). But it is not an array of pointers to doubles, which is what the double** type actually means: Dereference a double** and you get a double*.

If you know that the second subscript is constant (in your case, 2), you can define calculate() to expect a N*2 array of doubles:

void calculate(double array[][2], int a)
{
    //Whatever
}

Or, if both dimensions can change, just take a straight double* and to the pointer math yourself (you'll probably have to cast your array to a double* when calling this, but it'll work):

void calculate(double* array, int a, int b)
{
    //To get array[i][j]:
    array[i * b + j];
}

Hope this helps!

幽蝶幻影 2024-12-13 08:34:29

使用Boost.MultiArray:

http://www.boost。 org/doc/libs/1_47_0/libs/multi_array/doc/user.html

它们支持视图、范围、迭代器,并且抽象了内存管理,但仍然保持高度可配置性。您可以像您期望的那样通过引用传递 multi_arrays 。

如果您想了解原始指针(或者您正在编写需要适应 64k 内存或其他内容的嵌入式应用程序),那么编写使用它们的代码是一个好主意。如果您想编写可维护的生产代码,那么最好使用 STL/Boost 并避免使用原始指针,除非不太可能需要大量阅读的代码。

Use Boost.MultiArray:

http://www.boost.org/doc/libs/1_47_0/libs/multi_array/doc/user.html

they support views, ranges, iterators and they abstract away memory management but still remain extremely configurable. You can pass multi_arrays by reference just like you'd expect.

if you want to learn about raw pointers (or you're writing an embedded application that needs to fit in 64k of memory or something), then it's a good idea write code that uses them. If you want to write maintainable production code, then it's a good idea to use STL/Boost and avoid raw pointers except in code that is unlikely to have to be read very much.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文