Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to dynamically create a two dimensional array?

C/C++ Programming

How to dynamically create a two dimensional array?

by  axter  Posted    (Edited  )
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).

[blue]Vector-Vector: [/blue]
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.


[blue]Vector-Vector with wrapper class: [/blue]
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;  
};

[blue]Custom class dynamic_2d_array:[/blue]
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;
}

[blue]pointer-pointer:[/blue]
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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top