Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Is there anyway to create a multidimensional grid structure in C++ if I don't know the number of dimensions in advance.

For example I want to store some k dimensional data in the memory, but I don't know the value of k at compile time, it is obtained as a user input at run time. Is there any way of doing this in C++ ?

Thanks in advance.
Posted
Updated 28-Jan-11 18:16pm
v2

There's dynamic memory allocation, hence you can do that. Beware, as k increases, you will quickly run out of memory.
:)
 
Share this answer
 
Yes, you can certainly do that in C++.

No, there isn't anything built in that will do it for you ready made.

As CPallini indicated, it will require the use of dynamic memory allocation.

There was a somewhat related question recently. Based on that, here is a possible starting point. Beware this is off-the-cuff with no attempt to compile. It contains no error checking. It may even contain glaringly obvious errors.

template<typename T>
class multiDimArray
{
public:
    multiDimArray( const std::vector<size_t> &dimensions ) :
        dimensions_( dimensions ), pContents(NULL);
    {
        size_t size = 1;
        std::vector<size_t>::const_iterator dimIter = dimensions_.begin();
        for ( ; dimIter != dimensions_.end(), ++dimIter )
        {
            size *= *dimIter;
        }
        pContents = new T[ size ];
    }

    ~multiDimArray( )
    {
        delete [] pContents;
    }

    T& operator()( const std::vector<size_t> &indicies )
    {
        std::vector<size_t>::const_iterator indexIter = indicies.begin();
        std::vector<size_t>::const_iterator dimIter = dimensions_.begin();

        // calculate offset into linear array
        size_t offset = 0;
        ++dimIter;  // first dimension isn't used in the calculation
        for ( ; dimIter != dimensions_.end(); ++dimIter, ++indexIter )
        {
            offset += *indexIter;
            offset *= *dimIter;
        }
        offset += *indexIter;  // add last index value

        return *(pContents + offset);
    }

    std::vector<size_t>size_type NumberOfDimensions() const
    {
        return dimensions_.size();
    }

private:
    T *pContents;
    std::vector<size_t> dimensions_;
};
 
Share this answer
 
Comments
Sandeep Mewara 29-Jan-11 2:04am    
Good answer!
Sounds like Basic Linear Algebra Library[^] fits the bill, supports sparse matrices.

Regards
Espen Harlinn
 
Share this answer
 
Thanks a lot for the answers.

Got to know that OpenCV2.0 has a similar implementation named MatND_
http://opencv.willowgarage.com/documentation/cpp/basic_structures.html#id3[^]
 
Share this answer
 
You should almost always allocate memory in a single allocation and then subdivide it for the dimension of your problem, using pointers if you want notional convenience... Otherwise, you are likely going to page fault and cache miss like crazy.

BLAS, BLIX++, LAPACK, etc. should all use this methodology.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900