简体   繁体   中英

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. 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. 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:

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

Using C++ arrays:

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

You can define your own matrix type with C++ arrays (needs 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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