An array of a certain size, used from the function that it initialized in

I am a high school student studying programming, and I have a problem, and I cannot figure out how to solve it.

I have an integer "x" and I want the matrix "mat" to have the size "x":

int mat[x][x];

But this only works in main()where I read x;

For example, if x == 5, the equivalent will be

int mat[5][5];

#include <iostream>
using namespace std;

int x;

int mat[x][x];

void f(int mat2[x][x])
{

}

int main()
{
    cin >> x;
    int m[x][x];
    f(m);
}

I wrote this short program to show where it works and it does not work.

error: array binding is not an integer constant before the token ']'

I have an error in a global declaration, in a declaration in a function void f. It compiles without errors in main();

What can I do to create a matrix sized xoutside the main function?

+4
5

++. , .

std::vector:

void f(std::vector<std::vector<int>>& mat)
{

}

int main()
{
    cin >> x;
    std::vector<std::vector<int>> m(x,std::vector<int>(x));
    f(m);
}

, .

x ?

- :

std::vector<std::vector<int>> foo() {
    cin >> x;
    std::vector<std::vector<int>> m(x,std::vector<int>(x));
    return m;
}

int main()
{
    std::vector<std::vector<int>> mat = foo();
}
+3

++ . - .

, 2 2 2 * 2 = 4 :

+-----------+-----------+-----------+-----------+
| map[0][0] | map[0][1] | map[1][0] | map[1][1] |
+-----------+-----------+-----------+-----------+

, .

width height, map[i][j] i*height + j. ++, Matrix:

#include <array>
template <typename T, size_t WIDTH, size_t HEIGHT>
class Matrix {
    std::array<T, WIDTH*HEIGHT> data;

public:    
    T& operator()(size_t i, size_t j) {
        return data[i*HEIGHT + j];
    }
    const T& operator()(size_t i, size_t j) const {
        return data[i*HEIGHT + j];
    }
};

, Matrix ' ( , . (ii) ). :

void fill(Matrix<int, 2, 2>& m) {
    m(0,0) = 0;
    m(0,1) = 1;
    m(1,0) = 2;
    m(1,1) = 3;
}

int main() {
    Matrix<int, 2, 2> m;
    fill(m);
    std::cout << m(1,0) << "\n";
}

(i): (line, column), [line][column], operator[] .

Live on coliru

(ii): (demo) std::vector of std::array - std::vector::resize().

+1

. . Gnu VLR

( ) , ( ).

: mat2 , .. void f(int mat2[x][x]) void f(int mat2[][x]).

0

++ 2D-, . A () 2D- - . :

  • *
  • operator[](int), i-
  • ( , ++ 11 ) .

:

template <typename T>
class Matrix {
    T *data;
    int width;
    int height;

public:
    // direct ctor
    Matrix(int h, int w): width(w), height(h) {
        data = new T[w * h];
    }

    //copy ctor
    Matrix(const Matrix& src): width(src.width), height(src.height) {
        data = new T[width * height];   // allocate and copy data array
        for (int i=0; i<width * height; i++) data[i] = src.data[i];
    }

    // move ctor
    Matrix(Matrix&& src): width(src.width), height(src.height) {
        data = src.data;  // steal original array in a move
        src.data = NULL;  // ensure no deletion will occur at src destruction
    }

    ~Matrix() {
        delete data;
        data = NULL;
    }

    // explicitely delete assignement operators
    Matrix& operator = (const Matrix&) = delete;
    Matrix& operator = (Matrix&&) = delete;

    T* operator[](int i) {
        //optionaly test 0 <= i < width
        return &data[i * width];
    }
};

int main()
{
    int w;
    std::cin >> x;
    Matrix<int> m(x, x);
    // you can then use m[i][j] as you would for a static 2D array
    ...
}

. , vector<vector<T> >. , , ( ...).

0
source

You can dynamically allocate memory for use, in c / C ++, it does not support the dynamic size of the distribution of static memory, so you just change your code as follows.

int x;
cin >>x;
int** mat = new int[x][x];
-1
source

Source: https://habr.com/ru/post/1671602/


All Articles