INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

C/C++ Programming

How to dynamically create a two dimensional array? by axter
Posted: 15 Dec 04 (Edited 28 Jan 05)

There are many methods for creating a two dimensional array, but this FAQ will touch on three methods. (vector-vector, dynamic_2d_array, and pointer-pointer).

Vector-Vector:
A two dimensional array can be crated with a vector of vector using the following method:

CODE

std::vector<std::vector<mytype> > my_2d_int_array(SizeX, std::vector<mytype>(SizeY));

Example code:
#include <iostream>
#include <vector>

int main(int argc, char* argv[])
{
    int SizeX = 3;
    int SizeY = 5;
    std::vector<std::vector<int> > my_2d_int_array(SizeX, std::vector<int>(SizeY));

    int x, y;

    for(x = 0;x < my_2d_int_array.size();++x)
    {
        for(y = 0;y < my_2d_int_array[x].size();++y)
        {
            my_2d_int_array[x][y] = x*10 + y;
        }
    }

    for(x = 0;x < my_2d_int_array.size();++x)
    {
        for(y = 0;y < my_2d_int_array[x].size();++y)
        {
            std::cout << my_2d_int_array[x][y] << std::endl;
        }
    }
    return 0;
}
//The above method is simple, and requires no special class.


Vector-Vector with wrapper class:
The following code is a wrapper class for the Vector-Vector method.  This version of a dynamic_2d_array class has a resize() function to make it easy to resize a 2D array just like a vector.
This class is not compatible to a C-Style 2D static array, and it's not as efficient as the next class listed.

CODE

#include <vector>

template <typename T>
class dynamic_2d_array
{
public:
    dynamic_2d_array(){};
    dynamic_2d_array(int rows, int cols):m_data(rows, std::vector<T>(cols)){}
    inline std::vector<T> & operator[](int i) { return m_data[i];}
    inline const std::vector<T> & operator[] (int i) const { return m_data[i];}
    void resize(int rows, int cols){
        m_data.resize(rows);
        for(int i = 0;i < rows;++i) m_data[i].resize(cols);
    }
private:
    std::vector<std::vector<T> > m_data;  
};

Custom class dynamic_2d_array:
Using a custom class, a simple interface can be created that is a little more efficient then the vector-vector method, and itÆs more compatible with C-Style data in that it contains the array in one continuous block of memory.
See following link:
http://code.axter.com/dynamic_2d_array.h

The following example usage code can be used with the above custom class and with the vector wrapper class.

CODE

void Func(int x, int y)
{
 dynamic_2d_array <double> MyMatrix(x, y);
 MyMatrix[0][0] = 1;
 MyMatrix[0][1] = 2;
 MyMatrix[1][0] = 33;
 MyMatrix[1][1] = 44;
 cout << MyMatrix[0][0] << MyMatrix[0][1] << MyMatrix[1][0] << MyMatrix[1][1] << endl;
}

pointer-pointer:
The pointer-pointer method is more compatible with C code, in that no class is used to create the array.

CODE

template < typename T >
T **Allocate2DArray( int nRows, int nCols)
{
    T **ppi;
    T *pool;
    T *curPtr;
    //(step 1) allocate memory for array of elements of column

    ppi = new T*[nRows];

    //(step 2) allocate memory for array of elements of each row
    pool = new T [nRows * nCols];

    // Now point the pointers in the right place
    curPtr = pool;
    for( int i = 0; i < nRows; i++)
    {
        *(ppi + i) = curPtr;
         curPtr += nCols;
    }
    return ppi;
}

template < typename T >
void Free2DArray(T** Array)
{
    delete [] *Array;
    delete [] Array;
}

int main()
{
    double **d = Allocate2DArray<double>(10000, 10000);
    d[0][0] = 10.0;
    d[1][1] = 20.0;
    d[9999][9999] = 2345.09;
    Free2DArray(d);
}


For a completely C solution, check out the following link:
http://academia.hixie.ch/bath/ccourse/assessed-work-2/dynamic-2d-arrays.c
http://academia.hixie.ch/bath/ccourse/assessed-work-2/dynamic-2d-arrays.h

Back to Microsoft: Visual C++ FAQ Index
Back to Microsoft: Visual C++ Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close