我知道使用矢量要容易得多,但如果我想用C风格的数组来进行通用矩阵乘法,那么它只是在我的脑海中浮现,那将如何工作。 我在网上搜索并通过使用模板获得了一些帮助,但数组不会从函数返回。
// Example program #include <iostream> #include <string> using namespace std; template <typename T, size_t row1, size_t col1, size_t row2, size_t col2> typename multiply(T(&a)[row1][col1], T(&b)[row2][col2]) { if (row1 != col2) { cout << "Error! Rows of first matrix is not the same as Columns of second " "matrix, therefore Multiplication is NOT POSSIBLE!"; return 0; } else { T c[row1][col2]; for (size_t i = 0; i < row1; ++i) { for (size_t j = 0; j < col2; ++j) { c[i][j] = 0; for (size_t k = 0; k < col1; ++k) { c[i][j] += a[i][k] * b[k][j]; } } } return c; } } int main() { // C-style array int my_array[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}; int my_array2[5][2] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; int my_array3[2][2]; int c[2][2] = multiply(my_array, my_array2); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { cout << c[i][j] << " "; } cout << endl; } return 0; }因此,任何想法如何让这个代码工作?
I know using vectors is much easier but it just crossed my mind if i wanted to use C-style arrays to do generic matrix multiplication, how would that work. I searched online and got some help by using templates but the array won't return from the function.
// Example program #include <iostream> #include <string> using namespace std; template <typename T, size_t row1, size_t col1, size_t row2, size_t col2> typename multiply(T(&a)[row1][col1], T(&b)[row2][col2]) { if (row1 != col2) { cout << "Error! Rows of first matrix is not the same as Columns of second " "matrix, therefore Multiplication is NOT POSSIBLE!"; return 0; } else { T c[row1][col2]; for (size_t i = 0; i < row1; ++i) { for (size_t j = 0; j < col2; ++j) { c[i][j] = 0; for (size_t k = 0; k < col1; ++k) { c[i][j] += a[i][k] * b[k][j]; } } } return c; } } int main() { // C-style array int my_array[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}; int my_array2[5][2] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; int my_array3[2][2]; int c[2][2] = multiply(my_array, my_array2); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { cout << c[i][j] << " "; } cout << endl; } return 0; }So any idea how I can make this code work?
最满意答案
你不能像你那样在函数中返回0。
相反,将输出作为参数c传递,并将a和b作为常量。
函数的返回值是错误代码(0代表OK,-1代表KO)
顺便说一句,你的检查条件是错误的,我也纠正了它
// Example program #include <iostream> #include <string> using namespace std; template <typename T, size_t row1, size_t col1, size_t row2, size_t col2> int multiply(const T(&a)[row1][col1], const T(&b)[row2][col2], T(&c)[row1][col2] ) { if (col1 != row2) { cout << "Error! Columns of first matrix is not the same as Rows of second " "matrix, therefore Multiplication is NOT POSSIBLE!"; return -1; } else { //T c[row1][col2]; for (size_t i = 0; i < row1; ++i) { for (size_t j = 0; j < col2; ++j) { c[i][j] = 0; for (size_t k = 0; k < col1; ++k) { c[i][j] += a[i][k] * b[k][j]; } } } return 0; } } int main() { // C-style array int my_array[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}; int my_array2[5][2] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; int my_array3[2][2]; int a = multiply(my_array, my_array2,my_array3); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { cout << my_array3[i][j] << " "; } cout << endl; } return 0; }You can't either return 0 either return c in your function as you do.
Instead, pass the output as a parameter c and put a and b as const.
The return of the function is the error code (0 for OK, -1 for KO)
Btw, your check condition is wrong, I corrected it also
// Example program #include <iostream> #include <string> using namespace std; template <typename T, size_t row1, size_t col1, size_t row2, size_t col2> int multiply(const T(&a)[row1][col1], const T(&b)[row2][col2], T(&c)[row1][col2] ) { if (col1 != row2) { cout << "Error! Columns of first matrix is not the same as Rows of second " "matrix, therefore Multiplication is NOT POSSIBLE!"; return -1; } else { //T c[row1][col2]; for (size_t i = 0; i < row1; ++i) { for (size_t j = 0; j < col2; ++j) { c[i][j] = 0; for (size_t k = 0; k < col1; ++k) { c[i][j] += a[i][k] * b[k][j]; } } } return 0; } } int main() { // C-style array int my_array[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}; int my_array2[5][2] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; int my_array3[2][2]; int a = multiply(my_array, my_array2,my_array3); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { cout << my_array3[i][j] << " "; } cout << endl; } return 0; }更多推荐
发布评论