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
2 dimensional dynamic array
Moderator: Board moderators
-
- Learning poster
- Posts: 76
- Joined: Thu Mar 13, 2003 5:12 am
- Location: Russia
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
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
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)
Born from ashes - restarting counter of problems (800+ solved problems)
Is it possible to create a vector of vectors?
For example.
For example.
Code: Select all
vector<vector<int>> array;
-
- Learning poster
- Posts: 76
- Joined: Thu Mar 13, 2003 5:12 am
- Location: Russia
I don't know, but heytex seems to have done it. Look at his code.ec3_limz wrote:Is it possible to create a vector of vectors?
For example.
Code: Select all
vector<vector<int>> array;
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
typedef std::vector<int> IntVector;
typedef std::vector<IntVector> IntMatrix;
and so on
Best regards
DM
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)
Born from ashes - restarting counter of problems (800+ solved problems)
Yes it is okay, but you should leave a space between two '>'s, i.e.Experimenter wrote:I don't know, but heytex seems to have done it. Look at his code.ec3_limz wrote:Is it possible to create a vector of vectors?
For example.
Code: Select all
vector<vector<int>> array;
Code: Select all
vector<vector<int> > array;
Other ways
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
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