The task as a whole is to create two matrices with a custom dimension. However, the main problem for me is the syntax of two-dimensional arrays, which cannot (according to my meager knowledge) associate and create, based on user variables.

In C ++, even to the level of a teapot, it’s far away, with signposts, the sign is very superficial. Could you tell me in theory which articles can be considered, or which available methods for solving the problem?

ps - There are no limitations on methods in principle, I mean in theory, this can be done by a group of one-dimensional arrays, but this seems to me more sophisticated.

  • one
    Vector of vectors? std:vector - Alexander Petrov

1 answer 1

So, the matrix NxM ... Let for definiteness - integers ( int ).

First option, the most positive :) -

 vector<vector<int>> A(N,vector<int>(M)); 

Inversion semantics is standard - A[i][j];

Option two, almost C's :)

 int ** A = new int*[N]; for(int i = 0; i < N; ++i) A[i] = new int[M]; 

The appeal is the same.

The last option is to create a class with an array of NxM int 's, and redefine the operator [] - here are the sketches :

 class Matrix { Matrix(size_t N, size_t M):N(N),M(M), data(new int[N*M]){} ... int * operator[](size_t row) { return data + row*M; } size_t N, M; int * data; 

Then again, the semantics of the treatment will be standard.
I didn’t touch upon the issues of going beyond the limits of the range, freeing up memory in the second and third versions, or the need for constant and non-constant versions in the third, sketched only ideologically how to implement it.