如何在许多功能中使用相同的多维数组
How to use the same multidimensional array in many functions?
我是C 的初学者,老实说,我不知道如何解决一个任务。我必须使用二维数组创建一个矩阵。它的大小应取决于用户的输入(应该像... int matrix [m] [n],其中m和n是用户输入的数字)。然后,我应该用0到100的随机数填充并打印。好吧,我可以管理它。当我必须创建一个函数从此数组的行中找到最高数字时,问题就开始了。此功能的唯一参数可以是用户输入的行数(例如int函数(int i))。问题是我可以在多个功能中使用相同的数组吗?考虑到我是新手的事实,有什么办法可以做到这一点?还是任务形成不正确?对不起,很长的帖子,并提前感谢PS有人要求代码,所以这里是:
#include <iostream>
#include <cstdlib>
using namespace std;
int function1(int i)
{
//this is one of the functions I'm supposed to create-I described it earlier
}
int main()
{
int m,n;
cout<<"Matrix's size will be m rows and n columns. Please write m and n"<<endl;
cin>>m>>n;
int A[m][n];
int a,b;
for (a=0;a<m;a++)
{
for (b=0;b<n;b++)
{
A[a][b]=rand()%(100+1);
cout<<A[a][b]<<" ";
}
cout<<"n";
}
}
编辑:我要感谢大家的帮助。我问我的老师,他终于回应了。如果您好奇的话,他告诉我们(我没有听到)定义像INT [100] [100]或更高的数组,并且不允许用户输入任何更高的数字;)这不是最佳解决方案实用之一。再次感谢您!
在C 中执行此操作的正确方法是使用std :: vector或std :: array。
如果您由于人工要求而无法执行此操作,那么基于用户输入的C 中的2D数组根本就无法做到这一点。
cin >> m >> n;
...
int array [m][n]; // not possible
int** wannabe; // not an array
int array [m * n]; // not possible
您可以做的是"弄糊"的2D数组:
int* mangled = new int[m * n];
使用的示例:
class int_matrix
{
private:
int* mangled;
size_t rows;
size_t cols;
public:
int_matrix(size_t row, size_t col)
:rows(row),
cols(col)
{
mangled = new int[row * col];
}
int highest_in_row (size_t row)
{
...
}
};
请注意,此代码要求您遵循三个规则。
在C中,您只会通过编写int array[m][n]
来优雅地解决了这一点,但是您正在使用C ,因此无法做到。
您可以将您的功能包装到类中。在该课程中,您可以将数组作为成员变量。
class A {
int **matrix;
public:
A(int rows, int columns) {
matrix = new int*[rows];
for(int i = 0; i < rows; ++i)
matrix[i] = new int[columns];
}
int function(int i); //you can use your matrix in this function
}
如果您不能使用类,则可以使用全局变量。
在file.cpp
中 int **matrix;
int function(int i) {
//Do Something
}
//With rows the number of rows and columns the number of columns
//You can take these as parameters
int main() {
matrix = new int*[rows];
for(int i = 0; i < rows; ++i)
matrix[i] = new int[columns];
function(42);
}
如果您声明了像int int A[m][n];
这样的矩阵,其中 m
和 n
不是 const
,则不能将其传递给函数。有两种修复方法:
1)声明具有int A[10][10];
等常量大小的矩阵。在这种情况下,找到Max的功能将如下所示:
int max_in_row(int matr[10][10], int row) {
int max = 0;
for (int col = 0; col < 10; ++col)
if (matr[row][col] > max)
max = matr[row][col];
return max;
}
您可以找到简单的最大值作为int max = max_in_row(A, <row you want>);
2)(如果您不知道尺寸)将矩阵声明为数组:
int **A = new int*[n];
for (int i = 0; i < n; ++i)
A[i] = new int[m];
// fill A like you did
然后该功能看起来像
int max_in_row(int **matr, int row, int m) {
int max = 0;
for (int col = 0; col < m; ++col)
if (matr[row][col] > max)
max = matr[row][col];
return max;
}
您可以通过int max = max_in_row(A, <row you want>, m);
以下不是标准的C ,因为仅当编译器支持可变长度阵列时,它才能起作用。VLA是在C99中引入的,并在C11中进行了可选,但从未在C 标准中引入 - 但有些编译器即使在C 模式下也支持它。
黑客将将矩阵地址存储为全局void *
,并将其投放到函数内部的VLA的适当指针。需要此黑客攻击,因为在全局声明的那一刻,您不知道矩阵的列数。
#include <iostream>
#include <cstdlib>
void *Matrix;
int Columns;
using namespace std;
int function1(int i)
{
typedef int MAT[Columns]; // BEWARE!!! VLA is not standard C++
MAT *mat = static_cast<MAT *>(Matrix);
int mx = mat[i][0];
for(int j=0; j<Columns; j++) {
cout << " " << mat[i][j];
if (mat[i][j] > mx) mx = mat[i][j];
}
std::cout << endl;
return mx;
}
int main()
{
int m,n;
cout<<"Matrix's size will be m rows and n columns. Please write m and n"<<endl;
cin>>m>>n;
int A[m][n]; // BEWARE!!! VLA is not standard C++
int a,b;
for (a=0;a<m;a++)
{
for (b=0;b<n;b++)
{
A[a][b]=rand()%(100+1); // Note that I now use a and b here !
cout<<A[a][b]<<" ";
}
cout<<"n";
}
Matrix = static_cast<void *>(A);
Columns = n;
cout << "Enter row number to process: ";
cin >> a;
b = function1(a);
cout << "Max of row " << a << " is " << b << endl;
return 0;
}
不是真正的c - ish,但至少它会编译并通过Clang版本3.4.1
- 指向多维数组C++的指针
- C++使用数组作为多维数组,尽管将其初始化为带有指针的 1D
- 错误:字符数组的初始值设定项太多
- 使用递归模板动态分配的多维数组
- 多维数组 C++ 中数组下标的类型"int[int]"无效
- 如何实现删除数组的功能?
- 共享_ptr中多功能数组的类型是什么
- 可用于任何大小的数组的功能
- 数组排序功能
- 如何在许多功能中使用相同的多维数组
- 数组和功能C 的麻烦
- 使用自定义比较功能对静态多维数组进行排序
- 简单的C++字符数组加密功能 - 段故障
- 分段故障当char数组在功能中声明时
- 在C++中删除带指针的数组——多维和多指针数组
- c++多维数组多个数据类型
- 在嵌入式系统上将数组排序功能从c++移植到c
- 内存堆问题C++,具有动态分配多维数组的功能
- 动态数组打印功能
- const char数组多次初始化c++