2 dimensional dynamic array

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
heytex
New poster
Posts: 1
Joined: Mon May 26, 2003 10:11 am

2 dimensional dynamic array

Post by heytex »

I found it's quite frequently for me to use dynamic array.
usually,I use vector.
However, there are times that 2 dimensional dynamic array is needed.
This is a sample:
[cpp]
#include<vector>
#include<iostream>

using namespace std;

int main(void){
vector< vector<int> > column;
vector<int> rowTmp;
int i;
//build column by puting rowTmp in it.
for(i=0;i<10;i++){
column.push_back(rowTmp);
}
//put some data in row 3 (0,1,2).
for(i=5;i<10;i++){
column[2].push_back(i);
}
//access data in row.
for(i=0;i<column[2].size();i++){
cout<<column[2]<<" ";
}
}
[/cpp]

I was wondering how other people achieve this.
Is there any better way ?
thx

Experimenter
Learning poster
Posts: 76
Joined: Thu Mar 13, 2003 5:12 am
Location: Russia

Post by Experimenter »

I am doing it this way.
[cpp]
int **array;
int N = M = 0xf;
int i;
// create an array;
array = new int* [N];
for(i = 0;i< N; i++)
array = new int [M];

//delete an array
for(i = 0;i< N; i++)
delete [] array;
delete [] array;
[/cpp]
so, that all.

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

I prefer second way, but I use C and I don;t use vectors ...
But I think that first way is very good too :)
First way is nicer when we don't know sizes of tables, but time complexity of both of them should be the same

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

Post by ec3_limz »

Is it possible to create a vector of vectors?

For example.

Code: Select all

vector<vector<int>> array;

Experimenter
Learning poster
Posts: 76
Joined: Thu Mar 13, 2003 5:12 am
Location: Russia

Post by Experimenter »

ec3_limz wrote:Is it possible to create a vector of vectors?

For example.

Code: Select all

vector<vector<int>> array;
I don't know, but heytex seems to have done it. Look at his code.

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

typedef std::vector<int> IntVector;
typedef std::vector<IntVector> IntMatrix;

and so on

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

User avatar
cytse
Learning poster
Posts: 67
Joined: Mon Sep 16, 2002 2:47 pm
Location: Hong Kong
Contact:

Post by cytse »

Experimenter wrote:
ec3_limz wrote:Is it possible to create a vector of vectors?

For example.

Code: Select all

vector<vector<int>> array;
I don't know, but heytex seems to have done it. Look at his code.
Yes it is okay, but you should leave a space between two '>'s, i.e.

Code: Select all

vector<vector<int> > array;

suman
New poster
Posts: 45
Joined: Fri Oct 19, 2001 2:00 am
Contact:

Other ways

Post by suman »

Hi,
Yes there are other ways of creating 2D arrays of your own. But It is obviously painful than the two options described in the previous posts. Here is the link of the book of algorithm in which Dr. Bruno described how to do so in C++ , java or C#.

The website is http://www.brpreiss.com

I previously posted such an implementation in the algorithm section :
http://acm.uva.es/board/viewtopic.php?t=1776

-- Mahbub Murshed Suman

Post Reply

Return to “C++”