I am new in programming and my problem is probably notorious but still is a problem.
I want build class which can produce object which contain two dimensional dynamic array and member function which fill that array.
Question is where I should to create the array, in function or in constructor.
Cod of array is:

int sizeP, stupaca;
char **polje; //dinamicka alokacija polja s pozivacem na pokazivac
polje = new char*[sizeP];
for (int i = 0; i < sizeP; i++) 
{
polje[i] = new char[stupaca + 1];
polje[i][0] = stupaca;
for (int j = 1; j <= stupaca; j++)
polje[i][j] = i + 1 + j / 100.; 
}

2. question: do I need copy constructor? Probably yes, but I don’t know create it.
3.Can I delete the array in destructor with following cod:

for (int i = 0; i < sizeP; i++)
delete [] polje[i];
delete [] polje;

Thenx

Member Avatar for iamthwee

Well, when someone says dynamic array my knee jerk reaction would be use <vector> from the STL. However, I'm not too sure about 2d vectors although I guess its inherent structure would probably be the same.

If you're intent is to build this from scratch for fun then go for it, although you probably haven't found my reply useful.

hmmm

i hope this can help u a little....

#include<iostream>
using namespace std ;

class DynamecArray2D ;
class DynamecArray2D {
	char **pArray ;
public:
	DynamecArray2D() ;
	DynamecArray2D(int iRow, int iColumn) ;
	DynamecArray2D(DynamecArray2D &) ;  //copy constructor
	~DynamecArray2D() ;
};
DynamecArray2D::DynamecArray2D() {
	pArray = NULL;
}
DynamecArray2D::DynamecArray2D(int iRow, int iColumn)  {
	pArray = new char*[iRow] ;
	for(int i=0; i<iRow; ++i) {
		pArray[i] = new char[iColumn] ;
		pArray[i][iColumn-1] = NULL ;
	}
}
DynamecArray2D::DynamecArray2D(DynamecArray2D &objDynamecArray2D) {  //copy constructor
	char **p=NULL;
	int i=0,j=0,iColumn;
	p=objDynamecArray2D.pArray ;
	while(*(p+i)) {
		++i;
	}
	pArray = new char*[i] ;
	iColumn = strlen(objDynamecArray2D.pArray[0]) ;
	for(j=0; j<i; ++j) {
		pArray[j] = new char[iColumn+1] ;
		pArray[j][iColumn-1] = NULL ;
	}
	i=0;
	p=objDynamecArray2D.pArray ;
	while(*(p+i)) {
		strcpy(*(pArray+i),*(p+i));
		++i;
	}
}
DynamecArray2D::~DynamecArray2D() {
	int i=0;
	while(pArray[i]) {
		if(pArray[i]) {
			delete pArray[i] ;
		}
		++i;
	}
	if(pArray){
		delete []pArray ;
	}
}

>I'm not too sure about 2d vectors although I guess its inherent structure would probably be the same.

std::vector<std::vector<T> > matrix;

std::vector is designed to work the way people expect even in multiple dimensions.

Thank you I’ll try with that. I think this is it!

I don't use this for vectors I’m trying create from one big string many small strings, and place it in array.

Member Avatar for iamthwee

>std::vector<std::vector<T> > matrix;

Thanx _jsw, I was wondering that but I didn't know 4 sure. Do you have an example...

>I don't use this for vectors I’m trying create from one big string many small strings, and place it in array.

Well unless, you just missed the above, you can use a 2D vector to do exactly that. Unless, like I said before ,you're just doing that for fun, practise or just to complicate matters?

>Do you have an example...

#include <iostream>
#include <vector>

int main()
{
  typedef std::vector<std::vector<int> > mat_t;

  mat_t mat ( 10, std::vector<int> ( 10, 0 ) );

  for ( mat_t::size_type i = 0; i < mat.size(); i++ ) {
    for ( mat_t::size_type j = 0; j < mat[i].size(); j++ ) {
      if ( j % 2 != 0 )
        mat[i][j] = j;
    }
  }

  for ( mat_t::size_type i = 0; i < mat.size(); i++ ) {
    for ( mat_t::size_type j = 0; j < mat[i].size(); j++ )
      std::cout<< mat[i][j] <<' ';
    std::cout<<'\n';
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.