简体   繁体   English

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

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

I happened to need an n by n matrix, each of its elements is an integer array of length m. 我碰巧需要一个n×n矩阵,它的每个元素都是一个长度为m的整数数组。 I was wondering what is the efficient method to create such matrix. 我想知道创建这种矩阵的有效方法是什么。 By the way, I don't want to use the vectors here; 顺便说一句,我不想​​在这里使用向量。 I am interested to do this using c++ arrays. 我有兴趣使用c ++数组执行此操作。 Furthermore, how I would be able to change the element of such matrix after creating one. 此外,在创建矩阵之后,如何更改矩阵的元素。 Thank you for your time. 感谢您的时间。

Using C-style arrays: 使用C样式的数组:

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

Using C++ arrays: 使用C ++数组:

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

You can define your own matrix type with C++ arrays (needs C++14): 您可以使用C ++数组定义自己的矩阵类型(需要C ++ 14):

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

And, to use it: 并且,要使用它:

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