There is an example of creating a dynamic two-dimensional array:

float **Matrix = new float*[SizeOfMatrix]; for(i = 0; i<SizeOfMatrix;i++) { Matrix[i] = new float[SizeOfMatrix]; } 

SizeOfMatrix - matrix size. And how to declare a class to create objects like this matrix?

    3 answers 3

     class MyType { float **Matrix; int Size; public: MyType(int SizeOfMatrix) { Size=SizeofMatrix; float **Matrix = new float*[SizeOfMatrix]; for(int i = 0; i<SizeOfMatrix;i++) { Matrix[i] = new float[SizeOfMatrix]; } } ~MyType() { for(int i = 0; i<Size;i++) { delete [](Matrix[i]); } delete [](Matrix); } }; 

    UPD: Sorry, wrote the wrong memory release. Fixed.

    • Yes, right. - Daniel Pavlov
    • one
      I’m just trying to adapt to my code, to access the object by indices ... it doesn’t work - Daniel Pavlov
    • 2
      delete [] !!! - Costantino Rupert
    • one
      delete ** [] ** (Matrix [i]); if I'm not mistaken - fogbit
    • 2
      @ Daniel Overload the operator()(std::size_t i, std::size_t j) statement operator()(std::size_t i, std::size_t j) . - Costantino Rupert

    The canonical implementation of the matrix class in modern C++ :

     template <typename Y> struct Matrix { public: Matrix(std::size_t width, std::size_t height) : holder_(new Y[width * height]), width_(width), height_(height) { } const Y& operator()(std::size_t i, std::size_t j) const { // Можно и 'throw', но подсадит производительность. assert(i < width_ && j < height_); return *(holder.get() + j * width + i); } Y& operator()(std::size_t i, std::size_t j) { assert(i < width_ && j < height_); return *(holder.get() + j * width + i); } // Остальные операции. private: std::unique_ptr<Y> holder_; std::size_t width_; std::size_t height_; } 

    I also note that to optimize complex calculations, you can use their proxying with the help of expression templates , which very well underlines the advantages of patterned C++ . Implementation details (and much more, of course) can be found at boost :: ublas.

    • Well done, it will be a bit early for man only - skegg
    • I hesitate to ask ... And why is "modern C ++" better than this <strike> modern </ strike> code than the implementation given above? <br> simple <b> familiar and understandable OOP </ b> is no longer in vogue ? <br> <br> I ask because if I don’t know much and (or) don’t understand - I would like to fix it ... - Zowie
    • @AlexWindHope Well, at least by the fact that in my case memory allocation is performed in one place in the constructor. And the person who wrote the code above (due to the use of raw pointers) , for example, in the first version incorrectly freed the memory. Well, plus there are little things like support for any type of data in the matrix and different dimension'ы . - Costantino Rupert
    • @AlexWindHope What do you mean by the phrase "familiar and understandable OOP" and where, roughly speaking, do you find the OOP here and in the answer above? - Costantino Rupert
    • Those. due to the fact that someone incorrectly freed the memory of the OOP in C + <strike> atstoy </ strike> does not fit? <br> And you didn’t answer about “modernity” - I'm very interested, I’m not out of evil 2nd I ask again :) - Zowie

    The author did not cite the most efficient variant of a dynamic array, if he is certainly interested in efficiency. I quote a class similar to the answer above, with a different way of allocating memory and an access method:

     class Matrix { float* data; int size; public: Matrix(int matSize) { size=matSize; data=new float[size*size]; } ~Matrix() { delete[] data; } float* operator[](int row) { return data+row*size; } }; 

    This code is both shorter and faster.

    Use this:

     Matrix mat(5); mat[2][4]=3242.542; mat[3][1]=3.1415;