繁体   English   中英

C ++中具有数组元素的矩阵

[英]A matrix with array elements in c++

我碰巧需要一个n×n矩阵,它的每个元素都是一个长度为m的整数数组。 我想知道创建这种矩阵的有效方法是什么。 顺便说一句,我不想​​在这里使用向量。 我有兴趣使用c ++数组执行此操作。 此外,在创建矩阵之后,如何更改矩阵的元素。 感谢您的时间。

使用C样式的数组:

int arr [n][n][m];

使用C ++数组:

std::array<std::array<std::array<int,n>,n>,m> arr;

您可以使用C ++数组定义自己的矩阵类型(需要C ++ 14):

template <unsigned N, unsigned M, typename T = int>
using Matrix = std::array<std::array<std::array<T, N>, N>, M>;

并且,要使用它:

Matrix<10, 3> mat1;           // T = int
Matrix<10, 3, unsigned> mat2; // T = unsigned

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM