如何在c ++中返回数组 [英] How to return an array in c++
问题描述
如何从函数返回数组?我的代码是
How should I return an array from a function? My code is
float ClassArray::arr_sub(float a[100][100], float b[100][100]) {
int i,j;
for(i = 1; i < 10; i++) {
for(j = 1; j < 10; j++){
f[i][j]=b[i][j]-a[i][j];
}
}
return f;
}
,并且从这个函数返回的f应该赋值给另一个数组g一些其他类。
and the f returned from this function should be assigned to another array g declared in some other class.
float g[100][100];
g= cm.arr_sub(T,W);
但是在构建类时,它表示 float to float的不兼容类型分配[100] [100]
。
but while building the classes, it says incompatible type assignment of float to float[100][100]
.
推荐答案
我的答案在这里另一个关于数组的问题解释了为什么你不想使用数组。
My answer here to another question on arrays explains why you don't want to use arrays.
正如我在答案中所说,你不能像你一样分配一个数组:
As I say in that answer you can't assign an array like you're trying:
float g[100];
g = foo(); // illegal, assigning to arrays is not allowed
数组上的另一个奇怪的限制是,不允许从函数返回它们:
Another of the weird restrictions on arrays is that you're not allowed to return them from functions:
float foo()[100]; // illegal, returning an array from a function is not allowed
还要注意,像 float arr_sub(float a [100] [100])
你可能认为你是通过值传递数组,但实际上,调用另一个怪异的异常数组。在C和C ++中,只要你将一个函数的形式参数声明为一个数组,类型就从'array'调整为'指向数组的元素类型'。
Also note that when you declare a function like float arr_sub(float a[100][100])
you might think you're passing an array by value, but in fact that invokes another of the weird exceptions made for arrays. In C and C++, whenever you declare a formal parameter of a function to be an array, the type is adjusted from 'array' to 'pointer to the array's element type'.
因为数组的行为不像他们应该的,所以应该使用std :: array或std :: vector:
Since arrays don't behave like they ought, you should instead use std::array or std::vector:
std::array<float,100> foo(); // works
std::array<float,100> g;
g = foo(); // works
要做多维数组,您可以使用:
To do multi-dimentional arrays you can use:
std::array<std::array<float,100>,100> g;
虽然这有点麻烦,所以你可以typedef它:
Though that's a bit cumbersome so you can typedef it:
typedef std::array<std::array<float,100>,100> Matrix;
Matrix ClassArray::arr_sub(Matrix a, Matrix b) {
...
}
Matrix g;
g = cm.arr_sub(T,W);
如果你有一个支持C ++ 11的编译器,你甚至可以做一个模板类型别名:
And if you have a compiler that supports C++11 you can even do a template type alias:
template<typename T,int Rows,int Columns>
using Matrix2d = std::array<std::array<T,Columns>,Rows>;
Matrix2d<float,100,100> g;
性能说明
有一个原因你可能不想返回一个std :: array的值。如果数组很大,那么在将数据从返回值复制到您分配给它的变量时,可能会存在显着的性能成本。如果这证明是一个问题,那么std :: array的解决方案与其他大型类型的解决方案相同;使用'out'参数而不是按值返回。
Note on performance
There is one reason you might not want to return an std::array by value. If the array is large then there may be a signficant performance cost in copying the data from the return value into the variable you assign it to. If that ever proves to be a problem for you, then the solution with std::array is the same as it would be for other large types; use an 'out' parameter instead of returning by value.
void arr_sub(Matrix a, Matrix b, Matrix &result);
Matrix g;
arr_sub(T,W,g);
这不适用于std :: vector,因为std :: vector可以利用move语义以避免复制其所有元素。
This doesn't apply to std::vector because std::vector can take advantage of move semantics to avoid having to copy all its elements.
这篇关于如何在c ++中返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!