declaration of size of vector<vector<int> >

E

Erik Borgstr?m

Hi,

Yes, I have followed some of the discussion on vector<vector<int> >,
but perhaps I'm just blind. I want to use a vector<vector<int> > of
outer size n and inner size m. Are these correct ways to achieve this?

1.
vector<vector<int> > v(n);
for(int i=0;i<n;i++){
v = vector<int>(m);
}

2.
vector<int> smallvec(m);
vector<vector<int> > v(n, smallvec);

3.
vector<vector<int> > v(n, vector<int>(m));

Also, what's the preferred way?

thanks,
Erik
 
U

Unforgiven

Erik said:
Hi,

Yes, I have followed some of the discussion on vector<vector<int> >,
but perhaps I'm just blind. I want to use a vector<vector<int> > of
outer size n and inner size m. Are these correct ways to achieve this?

1.
vector<vector<int> > v(n);
for(int i=0;i<n;i++){
v = vector<int>(m);
}

2.
vector<int> smallvec(m);
vector<vector<int> > v(n, smallvec);

3.
vector<vector<int> > v(n, vector<int>(m));


As far as I can see, they're all correct.
Also, what's the preferred way?

I'd prefer the last one, as it avoids the need of creating any temporary
variable names, and is only one line.

I wouldn't use number 2 when defining a global variable, since you'd end up
with a global variable smallvec that never gets used after v is constructed.
 
A

Alberto Barbati

Erik said:
1.
vector<vector<int> > v(n);
for(int i=0;i<n;i++){
v = vector<int>(m);
}

2.
vector<int> smallvec(m);
vector<vector<int> > v(n, smallvec);

3.
vector<vector<int> > v(n, vector<int>(m));


They are all correct, but they all copy n times a vector of size m
filled with zeros. This is a waste of time. Indeed, solution 1 is even
worse, as it wastefully creates and destroys n temporary vectors of size
m (solution 2 and 3 require only one temporary).
Also, what's the preferred way?

Dunno, but I would use this:

vector<vector<int> > v(n);
for(int i=0;i<n;i++)
v.resize(m);

Alberto
 
E

Erik Borgstr?m

Hi Alberto and unforgiven,

Thanks a lot for your advices. So I guess I'll use the 'resize' alternative :)

/erik

Alberto Barbati said:
Erik said:
1.
vector<vector<int> > v(n);
for(int i=0;i<n;i++){
v = vector<int>(m);
}

2.
vector<int> smallvec(m);
vector<vector<int> > v(n, smallvec);

3.
vector<vector<int> > v(n, vector<int>(m));


They are all correct, but they all copy n times a vector of size m
filled with zeros. This is a waste of time. Indeed, solution 1 is even
worse, as it wastefully creates and destroys n temporary vectors of size
m (solution 2 and 3 require only one temporary).
Also, what's the preferred way?

Dunno, but I would use this:

vector<vector<int> > v(n);
for(int i=0;i<n;i++)
v.resize(m);

Alberto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top