在我的项目中,我有这些文件:
functions.h
functions.cc
main.cc
我试图以这种方式将 Matrix 作为指针传递给函数:
main.cc
// Size -> const short Size = 10;
int mtr1[Size][Size];
matrix_insert((int *)mtr1);
函数.h
void matrix_insert(int *mtr);
函数.cc
void matrix_insert(int *mtr) {
short i, j;
for (i = 0; i < Size; i++) {
for (j = 0; j < Size; j++) {
std::cin >> *(mtr + i * Size + j);
}
}
}
这确实有效,但我不喜欢这种方式...
有没有更好的方法?
谢谢!
编辑: 可以用 vector 模拟矩阵吗?
最佳答案
如果你真的想使用C数组,那么你可以这样做:
main.cc
int mtr1[Size][Size];
matrix_insert(mtr1);
函数.h
const short Size = 10;
void matrix_insert(int mtr[Size][Size]);
函数.cc
void matrix_insert(int mtr[Size][Size]) {
short i, j;
for (i = 0; i < Size; i++) {
for (j = 0; j < Size; j++) {
std::cin >> mtr[i][j];
}
}
}
关于c++ - 将 Matrix 作为指向函数的指针传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13211815/