使用模板返回一个通用C风格的数组(Return a generic C

编程入门 行业动态 更新时间:2024-10-15 16:25:19
使用模板返回一个通用C风格的数组(Return a generic C-style array using templates)

我知道使用矢量要容易得多,但如果我想用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; }

更多推荐

本文发布于:2023-07-30 16:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338889.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   模板   风格   generic   Return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!