3

I need to create 2d array in c++.

I can't do it by int mas= new int[x][y]; or auto mas= new int[x][y]; I need to create an array dynamically like:

int x,y
auto mas= new int[x][y];//error - must be const.

Please help me.

Mat
  • 202,337
  • 40
  • 393
  • 406
SevenDays
  • 3,718
  • 10
  • 44
  • 71
  • 3
    You should get a good book: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list –  Oct 30 '11 at 09:33
  • 1
    possible duplicate of [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – Mat Oct 30 '11 at 09:35
  • related: http://stackoverflow.com/questions/3904304/3d-array-c-using-int-operator/3904484#3904484 – justin Oct 30 '11 at 10:01

5 Answers5

4

The C++ tool for creating dynamically sized arrays is named std::vector. Vector is however one-dimensional, so to create a matrix a solution is to create a vector of vectors.

std::vector< std::vector<int> > mas(y, std::vector<int>(x));

It's not the most efficient solution because you pay for the ability to have each row of a different size. You you don't want to pay for this "feature" you have to write your own bidimensional matrix object. For example...

template<typename T>
struct Matrix
{
    int rows, cols;
    std::vector<T> data;

    Matrix(int rows, int cols)
      : rows(rows), cols(cols), data(rows*cols)
    { }

    T& operator()(int row, int col)
    {
        return data[row*cols + col];
    }

    T operator()(int row, int col) const
    {
        return data[row*cols + col];
    }
};

Then you can use it with

 Matrix<int> mat(y, x);
 for (int i=0; i<mat.rows; i++)
   for (int j=0; j<mat.cols; j++)
     mat(i, j) = (i == j) ? 1 : 0;
6502
  • 112,025
  • 15
  • 165
  • 265
4
int x,y;
x =3;
y = 5;
int ** mas = new int*[x];
for (int i=0;i<x;i++)
{
   mas[i] = new int[y];
}

I think something like this. Don't forget

for(int i=0;i<x;i++)
   delete[] mas[i];
delete[] mas;

at the end.

Kotodid
  • 879
  • 6
  • 18
2

My advice would be to avoid the pain of multidimensional arrays in the first place and use a struct.

struct Point {
    int x;
    int y;
}

int points = 10;
Point myArray[points];

Then to access a value:

printf("x: %d, y: %d", myArray[2].x, myArray[2].y);

Depends on exactly what you're trying to achieve, though.

Ant
  • 4,890
  • 1
  • 31
  • 42
  • 1
    An array of pairs is completely different from a matrix. In one case you have `2*n` values, in the other `n*m` values. – 6502 Oct 30 '11 at 09:54
2

You can do the manipulations yourself.

int* mas = new int[x*y];

and access [i,j] by:

mas[i*y + j] = someInt;
otherInt = mas[i*y +j];
dalle
  • 18,057
  • 5
  • 57
  • 81
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
  • 1
    @Charles Bailey: is correct. usually when you define matrix[rows][columns] then the compiler can translate matrix[x][y] to matrix[x*columns +y] and all will work good. but when you just cast it to int** he have no idea how to access matrix[x][y]. he must know the dimensions for that. – Roee Gavirel Oct 30 '11 at 14:19
  • Charles and Roee, thanks for correcting me. I compiled a test code but didn't run it - it indeed causes undefined behavior (namely, crashing...). It looks like I'm a little rusty on pointer arithmetics... – Eli Iser Oct 30 '11 at 14:25
0
std::vector<std::vector<int> >  mas(y, std::vector<int>(x));
ragnarius
  • 5,642
  • 10
  • 47
  • 68