If I dont know the size of both the dimensions of array and want to print a matrix using the following code
如果我不知道数组的两个维度的大小,想用下面的代码打印一个矩阵
void printAnyMatrix(int (*A)[], int size_A, int size_B)
{
for (int i = 0; i<=size_A; i++)
{
for (int j = 0; j<=size_B; j++)
printf("%d ", A[i][j]);
printf("\n");
}
printf("\n");
}
Compiler gives
error cannot convert ‘int (*)[(((unsigned int)((int)size_B)) + 1)]’ to ‘int ()[]’ for argument ‘1’ to ‘void printAnyMatrix(int ()[], int, int)
错误无法将' int(*)[((((((无符号int)size_B)) + 1)] '转换为' int ()[] ' for arguments ' 1 '转换为' void printAnyMatrix(int ()[], int, int)
3 个解决方案
#1
4
Use template
feature for such problems:
对此类问题使用模板特性:
template<typename T, unsigned int size_A, unsigned int size_B>
void printAnyMatrix(T (&Arr)[size_A][size_B])
{ // any type^^ ^^^ pass by reference
}
Now you can pass any 2D array to this function and the size will be automatically deduced in the form of size_A
and size_B
.
现在您可以将任何2D数组传递给这个函数,大小将自动以size_A和size_B的形式推断出来。
Examples:
例子:
int ai[3][9];
printAnyMatrix(ai);
...
double ad[18][18];
printAnyMatrix(ad);
#2
2
Simplify the signature: a pointer is simpler to read by humans.
简化签名:指针更容易被人类读取。
You also have an error in the loops: it's less then, not less or equal.
在循环中也有一个错误:它小于,而不是小于或等于。
void printAnyMatrix(int *A, int size_A, int size_B)<br />
{
for (int i = 0; i<size_A; i++)
{
for (int j = 0; j<size_B; j++)
printf("%d ", A[i*size_B + j]);
printf("\n");
}
printf("\n");
}
#3
1
If you want to print any matrix, maybe you will need to print both statically and dynamically allocated matrices. Function for statically-allocated matrices will look like the following
如果要打印任何矩阵,可能需要同时打印静态和动态分配的矩阵。静态分配矩阵的函数将如下所示
template <class T, int size_A, int size_B>
void printAnyMatrix(T (&A)[size_A][size_B])
{
for (int i = 0; i < size_A; i++)
{
for (int j = 0; j < size_B; j++)
std::cout<<A[i][j]<<' ';
std::cout<<'\n';
}
std::cout<<std::endl;
}
Function for dynamically-allocated matrices:
函数动态矩阵:
template <class T>
void printAnyMatrix(T **A, int size_A, int size_B)
{
for (int i = 0; i < size_A; i++)
{
for (int j = 0; j < size_B; j++)
cout<<A[i][j]<<' ';
cout<<'\n';
}
std::cout<<std::endl;
}
You can have both of them in the same translation unit, compiler will pick the one that fits to your matrix. (note: for statically-allocated matrices you need one parameter, while for dynamically-allocated - three)
你可以把它们放在同一个翻译单元中,编译器会选择适合你的矩阵的那个。(注:静态分配矩阵需要一个参数,动态分配矩阵需要三个参数)
Please consider using cout
instead of printf
if you work in C++. It is overloaded for all basic types, while printf
needs you to declare the type explicitly.
如果您使用c++,请考虑使用cout而不是printf。它对所有基本类型都是重载的,而printf需要您显式地声明类型。
If you still have compiler errors, please show the declaration of your matrix.
如果您仍然有编译错误,请显示您的矩阵的声明。
#1
4
Use template
feature for such problems:
对此类问题使用模板特性:
template<typename T, unsigned int size_A, unsigned int size_B>
void printAnyMatrix(T (&Arr)[size_A][size_B])
{ // any type^^ ^^^ pass by reference
}
Now you can pass any 2D array to this function and the size will be automatically deduced in the form of size_A
and size_B
.
现在您可以将任何2D数组传递给这个函数,大小将自动以size_A和size_B的形式推断出来。
Examples:
例子:
int ai[3][9];
printAnyMatrix(ai);
...
double ad[18][18];
printAnyMatrix(ad);
#2
2
Simplify the signature: a pointer is simpler to read by humans.
简化签名:指针更容易被人类读取。
You also have an error in the loops: it's less then, not less or equal.
在循环中也有一个错误:它小于,而不是小于或等于。
void printAnyMatrix(int *A, int size_A, int size_B)<br />
{
for (int i = 0; i<size_A; i++)
{
for (int j = 0; j<size_B; j++)
printf("%d ", A[i*size_B + j]);
printf("\n");
}
printf("\n");
}
#3
1
If you want to print any matrix, maybe you will need to print both statically and dynamically allocated matrices. Function for statically-allocated matrices will look like the following
如果要打印任何矩阵,可能需要同时打印静态和动态分配的矩阵。静态分配矩阵的函数将如下所示
template <class T, int size_A, int size_B>
void printAnyMatrix(T (&A)[size_A][size_B])
{
for (int i = 0; i < size_A; i++)
{
for (int j = 0; j < size_B; j++)
std::cout<<A[i][j]<<' ';
std::cout<<'\n';
}
std::cout<<std::endl;
}
Function for dynamically-allocated matrices:
函数动态矩阵:
template <class T>
void printAnyMatrix(T **A, int size_A, int size_B)
{
for (int i = 0; i < size_A; i++)
{
for (int j = 0; j < size_B; j++)
cout<<A[i][j]<<' ';
cout<<'\n';
}
std::cout<<std::endl;
}
You can have both of them in the same translation unit, compiler will pick the one that fits to your matrix. (note: for statically-allocated matrices you need one parameter, while for dynamically-allocated - three)
你可以把它们放在同一个翻译单元中,编译器会选择适合你的矩阵的那个。(注:静态分配矩阵需要一个参数,动态分配矩阵需要三个参数)
Please consider using cout
instead of printf
if you work in C++. It is overloaded for all basic types, while printf
needs you to declare the type explicitly.
如果您使用c++,请考虑使用cout而不是printf。它对所有基本类型都是重载的,而printf需要您显式地声明类型。
If you still have compiler errors, please show the declaration of your matrix.
如果您仍然有编译错误,请显示您的矩阵的声明。