K
K
Kirill2017-11-25 19:37:44
C++ / C#
Kirill, 2017-11-25 19:37:44

How to initialize an array of arrays of arrays?

double ***array;
How to initialize it correctly
Let's say if you need two arrays of arrays. And each of them has 3 arrays of 4 elements.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
C
Cyril, 2017-11-25
@mrbbfst

Here's what happened

void neural_network_lib::Weight::fSetMemWeights()	//+
{
  this->d->pppdWeights = new double**[this->d->iQuantLayer];
  for (int i = 0; i < this->d->iQuantLayer; i++)	
    this->d->pppdWeights[i] = new double*[this->d->piQuantNInLayer[i]];
  for (int i = 0; i < this->d->iQuantLayer; i++)
    for (int j = 0; j < this->d->piQuantNInLayer[i + 1]; j++)
      this->d->pppdWeights[i][j] = new double[this->d->piQuantNInLayer[i + 1]];
}

J
jcmvbkbc, 2017-11-25
@jcmvbkbc

Let's start with the fact that double ***array-- is not an array of arrays of arrays, but a pointer to a pointer to a pointer to double. An array of arrays of arrays is a double array[A][B][C]. It is initialized as usual:

double array [A][B][C] = {
    {
        {
            1., 2., 3., ...
        }, {
            ...
        }
    }, {
        ...
    }
};

A
Adamos, 2017-11-25
@Adamos

If the overhead is not critical - a nested vector.
If you want optimally - a class with two set / get functions and a constructor to which the dimensions are passed. Inside, the dimensions and a flat dynamic array are stored, the cell number in which is elementarily calculated at each call.

A
Alexander Taratin, 2017-11-27
@Taraflex

Hipster variant

#include <iostream>
#include <array>
#include <vector>

template <typename V, typename... T>
constexpr auto make_array(V &&v, T&&... t) -> std::array < V , sizeof...(T) + 1 >
{
    return {{v, std::forward<T>(t)... }};
}

int main()
{
    auto array = make_array(
        make_array(make_array(1.,2.,3.,4.),make_array(5.,6.,7.,8.),make_array(9.,10.,11.,12.)),
        make_array(make_array(14.,15.,16.,17.),make_array(18.,19.,20.,21.),make_array(22.,23.,24.,25.))
    );
    
    for(auto && v: array[0][2]){
        std::cout << v << ',';
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question