Multidimensional Array indexed by integer array I am in need of a multidimensional array that can be initialized easily with arbitrary dimensions. I have found this code here, which is beautiful, however poses some difficulties for me. its hard to initialize with arbitrary size and also difficult to access since I have to input each index using a comma to seperate it. 

In my application each array 'slot' has the same size. Furthermore it would be easy to work with, if I could index and access everything using an array, instead of having to input every index seperated by a comma. So I wrote the following code which provides something like that, however feels very heavy due to only using one large array. I am basically shifting the 'multidimensional' array layers into one huge array... abusing high valued indices. Is there a way to approve apon the code, making it faster, and using less memory and high value calculations? 

#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;array&gt;

using namespace std;


template &lt;class T, size_t depth, size_t each_size&gt;
class MultiArray
{
    array&lt;T, static_cast&lt;size_t&gt;( pow(each_size, depth) )&gt; multiArray;

public:
    MultiArray()
    { ; }

    void put(array&lt;size_t, depth&gt; indices, T thing_to_put)
    {
        size_t index = 0;
        int dimension_counter = 0;
        for (const size_t&amp; i : indices)
        {
                index += i * nearbyint(pow(each_size, dimension_counter));
                dimension_counter++;
        }

        this-&gt;multiArray[index] = thing_to_put;
    }

    void get(array&lt;size_t, depth&gt; indices, T &amp;thing_to_get)
    {
        size_t index = 0;
        int dimension_counter = 0;
        for (const size_t&amp; i : indices)
        {
                index += i * nearbyint(pow(each_size, dimension_counter));
                dimension_counter++;
        }
        thing_to_get = this-&gt;multiArray[index];
    }
};


if you would like you can find a slightly longer version of the code above with some debugging, simple testing and very basic error handling here. 

EDIT: I would like to find a very fast and efficiant way to do things! I would be happy about input on how to get a fast multidimensional array class :) Thank you all for your help!!!
You don't need to call pow() and calculate powers "from scratch" within each loop iteration - it's probably cheaper to multiply the value calculated during previous iteration as you are calculating consecutive powers of each_size:

size_t temp = 1;
for (const size_t&amp; i : indices)
{
    temp *= each_size;
    index += i * temp;
}


Maybe also come up with a better name for this temp variable than I did ;p

std::nearbyint() is also redunant since you are calculating integral powers of an integer, so your result is going to be an integer by definition.

Apart from this, if your array is really huge, consider using some other container that doesn't store data contiguously as with the fragmented memory there is a risk of not being able to allocate space for such a large object. std::deque might fit your needs, but it's certainly less efficient, so you have to weigh pros and cons. Another advantage would be dynamic size that adds more flexibility to the way your array is used (you can also get it with std::vector, but it's also a contiguous container).

Last but not least, there is no advantage to passing primitive types such as size_t by reference as the associated pointer that's copied is of similar size to the variable that would be copied by value.