|
-
March 10th, 2009, 09:22 AM
#4
Re: Declare a 2D Dynamic array
 Originally Posted by Lindley
Alternatives , each with slightly different properties:
Code:
//1
int **arr2d = new int*[rows];
for (i = 0; i < rows; i++)
arr2d[i] = new int[cols];
//and release
for (i = 0; i < rows; i++)
delete[] arr2d[i];
delete[] arr2d;
//2
std::vector< std::vector<int> > arr2d(rows, std::vector<int>(cols));
// no need to explicitly release
//3
std::vector<*int> arr2d(rows);
std::vector<int> arr2d_data(rows*cols);
for (i = 0; i < rows; i++)
arr2d[i] = &arr2d_data[i*cols];
Alright Thanks! One more question, suppose I make a matrix by using an array "mat[4][5]" 4 rows and 5 columns and I wish to fill the matrix with random alphabets, how will I do that? Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|