3

I have declared a 2D array in C++ using pointers.

int **matrix = new int*[5];
for(int i=0;i<5;i++)
{
    matrix[i] = new int[5];
}

Now as i have allocated memory to array 'matrix', Wondering how can i initialize all the array items in this format:

int arr[2][5] =
{
    {1,8,12,20,25},
    {5,9,13,24,26}
};
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Gautam Kumar
  • 941
  • 2
  • 16
  • 37

4 Answers4

2

You should use a std::vector instead of new[]. If your compiler supports it (the newer ones do), you can use an initializer list.

std::vector<std::vector<int>> matrix = { { 1, 2, 3 }, { 4, 5 } };
D Drmmr
  • 1,223
  • 8
  • 15
  • Always avoid using [>>](http://stackoverflow.com/questions/15785496/c-templates-angle-brackets-pitfall-what-is-the-c11-fix) in template arguments. Better use [> >](http://stackoverflow.com/questions/6695261/template-within-template-why-should-be-within-a-nested-template-arg). Although newer standards have provision for this. Your code is not [compilable](http://codepad.org/O0uiD0l5). Have you tried it? – Mohit Jain Apr 12 '14 at 08:56
  • 3
    @MohitJain [*It does compile*](http://ideone.com/pxRwb5), and your code doesn't because (a) you used g++ 4.1.2, and as today, the newest version is 4.8.1 (b) you did not `#include ` – milleniumbug Apr 12 '14 at 09:14
  • Yes C++11 has provision to handle >> and it compiles. On C++11, I would rather suggest using std::array. About not including vector, I did it deliberately because [codepad](http://codepad.org/) is a platform to quickly check things and not for some real coding. – Mohit Jain Apr 12 '14 at 09:19
  • @MohitJain a `vector` of `vectors` is semantically closer to OP's "2D array" than an `std::array`. This doesn't meant that it is necessarily a good thing to use, but there is no information in the question to make that call. – juanchopanza Apr 12 '14 at 09:38
  • I meant array of arrays. Anyways fine. And I am not talking about compiler versions, I am talking about C++(standards/specification) versions when I talk about C++11 or >> not working. – Mohit Jain Apr 12 '14 at 09:40
  • @MohitJain it's silly to do the `> >` and avoid `>>` he's using the initializer list constructor of `std::vector` if your compiler supports that then it also supports `>>`, so there's no reason to avoid it. – PeterT Apr 12 '14 at 12:30
1

Method suggested by you works only for fixed size arrays and not dynamic arrays. You need to initialize them by yourself(read from file, use loops etc). Or if you are sure about array dimension, use 2D array instead of dynamically allocating it.

Or you can use C++11.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
0

In C++11 you can use uniform initialization with operator new[]. For example:

auto arr = new int[2][5] {
    {1,8,12,20,25},
    {5,9,13,24,26}
};

Working on Clang 3.4

Ricky65
  • 1,657
  • 18
  • 22
0

The simplest way is to use nested std::vector(s) and an initializer_list.

Dynamically allocated multidimensional array are quite error prone. E.g. if you look closely at your example you'll see that the correct code for a [2][5] matrix is:

int **matrix = new int*[2];
for(int i(0); i < 2; ++i)
  matrix[i] = new int[5];

If you really want a matrix, probably neither std::vector<std::vector<int>> nor int** matrix are good layouts (the main problem is that you lose data locality).

A classic solution that works quite nicely is a wrapper around a single vector, that keeps track of the "shape" of the matrix being represented and provides an operator to access the data:

class matrix
{
public:
  matrix(unsigned rows, unsigned columns)
    : columns_(columns), data_(columns * rows)
  {}

  matrix(std::initializer_list<std::initializer_list<int>> lst)
    : matrix(lst.size(), lst.size() ? lst.begin()->size() : 0)
  {
    unsigned i(0), j(0);

    for (const auto &l : lst)
    {
      for (const auto &v : l)
      {
        operator()(i, j) = v;
        ++j;
      }

      j = 0;
     ++i;
    }
  }

  int &operator()(unsigned row, unsigned column)
  { return data_[row * columns_ + column]; }

private:
  unsigned columns_;
  std::vector<int> data_;
};

and you can write:

matrix m = {
  {1,8,12,20,25},
  {5,9,13,24,26},
};
manlio
  • 18,345
  • 14
  • 76
  • 126