Black
Black

Reputation: 20382

Initialize multidimensional array with zeros

In C++ you can initialize a one dimensional array with 0 with a code like this:

int myarray[100] = {0};

Is there a similar way for multidimensional arrays? Or am i forced to initialize it manually with for loops?

Upvotes: 9

Views: 53590

Answers (7)

Jan Christoph Terasa
Jan Christoph Terasa

Reputation: 5945

For "proper" multi-dimensional arrays (think numpy ndarray), there are several libraries available, for example Boost Multiarray. To quote the example:

#include "boost/multi_array.hpp"
#include <cassert>

int 
main () {
  // Create a 3D array that is 3 x 4 x 2
  typedef boost::multi_array<double, 3> array_type;
  typedef array_type::index index;
  array_type A(boost::extents[3][4][2]);

  // Assign values to the elements
  int values = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        A[i][j][k] = values++;

  // Verify values
  int verify = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        assert(A[i][j][k] == verify++);

  return 0;
}

See also: High-performance C++ multi-dimensional arrays

Upvotes: 1

yazyyyyy
yazyyyyy

Reputation: 21

You could use std::memset to initialize all the elements of a 2D array like this:

int arr[100][100]
memset( arr, 0, sizeof(arr) )

Even if you have defined the size via variables this can be used:

int i=100, j=100;
int arr[i][j]
memset( arr, 0, sizeof(arr) )

This way all the elements of arr will be set to 0.

Upvotes: 2

Volta Aaoi
Volta Aaoi

Reputation: 1

Using 2 vector containers:

std::vector<std::vector<int>> output(m, std::vector<int>(n, 0));

This way one can declare a 2D vector output of size (m*n) with all elements of the vector initialized to 0.

Upvotes: 0

Ajay jangid
Ajay jangid

Reputation: 1003

In C++, simply you can also do this way:-
int x = 10, y= 10; int matrix[x][y] = {}; and then the 2d-array will be initialized with all zeroes.

Upvotes: 0

whishky
whishky

Reputation: 416

use vector instead of array it will give you more flexibility in declaration and in any other operation

vector<vector<int> > myarray(rows,vector<int>(columns, initial_value));

you can access them same as you access array,

and if u still want to use array then use std::fill

Upvotes: 5

mziccard
mziccard

Reputation: 2178

You do it exactly the same way

int marr[10][10] = {0};

Edit:

This is a C solution. For a C++ solution you can go for:

int marr[10][10] = {};

These 2 solutions do not work for arrays that have size defined via variables. e.g.:

int i, j = 10;
int marr[i][j];

To initialize such an array in C++ use std::fill.

Upvotes: 23

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145457

A multidimensional array is an array of arrays.

The same general array initialization syntax applies.

By the way you can just write {}, no need to put an explicit 0 in there.

Upvotes: 8

Related Questions