如何在 C++ 中发送二维数组作为参考并返回?
另一个初级到中级的问题。我正在尝试将二维数组传递给 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么数组不能直接发送给函数?
Call
Class.calculate (*(input), a, b);
正在尝试取消引用input
,但您无法执行此操作,因为它不是指针。如果
calculate
定义为:您只需调用
Class.calculate(input, a, b)
即可。Why the array cannot be sent directly to a function?
Call
Class.calculate (*(input), a, b);
is trying to dereferenceinput
which you can't do as it is not a pointer.If the
calculate
is defined as:You can just call
Class.calculate(input, a, b)
.错了。你可以通过它。只需将
calculate
成员函数的第一个参数设置为指向指针的指针即可。并像这样称呼它 - Class.calculate(input, a, b);
Wrong. You can pass it. Just have the
calculate
member function's first argument to be pointer to a pointer.And call it like - Class.calculate (input, a, b);
还没人说起模板吗?我很失望!
实际上,您可以将其作为真实参考传递,而无需在 C++ 中将大小作为额外参数传递:
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++:
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.
如果没有看到
calculate()
的声明,很难确定,但我猜这是参数类型不匹配。您可以将数组作为double*
发送,但不能作为double**
发送 - 它是一个双精度数组(它们被放置在在内存中线性输出,即使编译器允许您将它们视为多维数组)。但它不是一个指向双精度的指针数组,这就是double**
类型的实际含义:取消引用double**
并且你得到一个double*
。如果您知道第二个下标是常量(在您的情况下为 2),则可以定义calculate() 来期望 N*2 双精度数组:
或者,如果两个维度都可以更改,则只需采用直接
double*
并自己进行指针数学运算(调用此函数时,您可能必须将数组转换为double*
,但它会起作用):希望这有帮助!
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 adouble*
, but not as adouble**
- 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 thedouble**
type actually means: Dereference adouble**
and you get adouble*
.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: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 adouble*
when calling this, but it'll work):Hope this helps!
使用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.