HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Generic multidimensional array in C++

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arraygenericmultidimensional

Problem

#include 
#include 
using std::cout;
using std::endl;

template
class Array
{
private:
    std::unique_ptr pointer;
    int size[dimension];
    int realSize;

public:
    Array()
    {
    }

    template
    Array(Ns... ns)
    : realSize(1)
    {
        create(1, ns...);
    }

private:
    template
    void create(int d, int n, Ns... ns)
    {
        realSize *= n;
        size[d - 1] = n;
        create(d + 1, ns...);
    }

    void create(int d)
    {
        pointer = std::unique_ptr(new T[realSize]);
    }

    int computeSubSize(int d) const
    {
        if (d == dimension)
        {
            return 1;
        }
        return size[d] * computeSubSize(d + 1);
    }

    template
    int getIndex(int d, int n, Ns... ns) const
    {
        return n * computeSubSize(d) + getIndex(d + 1, ns...);
    }

    int getIndex(int d) const
    {
        return 0;
    }

public:
    template
    T& operator()(Ns... ns) const
    {
        return pointer[getIndex(1, ns...)];
    }

    int getSize(int d = 1) const
    {
        return size[d - 1];
    }
};

int main()
{
    constexpr int N = 10;
    Array a(N);
    for (int i = 0; i  a2(N, N * 2);
    for (int i = 0; i < a2.getSize(1); ++i)
    {
        for (int j = 0; j < a2.getSize(2); ++j)
        {
            a2(i, j) = i + j + 2;
            if (a2(i, j) < 10)
            {
                cout << '0';
            }
            cout << a2(i, j) << ' ';
        }
        cout << endl;
    }
    return 0;
}


Output

```
1 2 3 4 5 6 7 8 9 10

02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21
03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22
04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
09 10 11 12 13 14 15 16 17 18 19 20 21

Solution

-
No need for an empty constructor in C++11:

Array()
{
}


You can just use a default constructor:

Array() = default;


-
It's a little confusing to have multiple public/private sections. Here, you can just put all the public code under the same keyword.

-
You could make your structure more useful by providing iterators. This will, for instance, allow you to use range based for-loops instead of plain ones for iterating through this structure.

-
You don't need std::endl if you just need newlines. Just output "\n" instead for this. More info about this can be found here.

-
It's unnecessary to have your own return 0 at the end of main() in C++. The compiler will provide this return for you.

Code Snippets

Array()
{
}
Array() = default;

Context

StackExchange Code Review Q#94148, answer score: 8

Revisions (0)

No revisions yet.