473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

declaration of size of vector<vector<int> >

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[i] = 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
Jul 22 '05 #1
3 2782
Erik Borgstr?m wrote:
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[i] = 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.

--
Unforgiven

Jul 22 '05 #2
Erik Borgstr?m wrote:
1.
vector<vector<int> > v(n);
for(int i=0;i<n;i++){
v[i] = 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[i].resize(m);

Alberto
Jul 22 '05 #3
Hi Alberto and unforgiven,

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

/erik

Alberto Barbati <Al************@libero.it> wrote in message news:<co********************@twister2.libero.it>.. .
Erik Borgstr?m wrote:
1.
vector<vector<int> > v(n);
for(int i=0;i<n;i++){
v[i] = 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[i].resize(m);

Alberto

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andreas Krueger | last post by:
Hi! I am fed up with vector<int> iv; iv.push_back(42); iv.push_back(9); iv.push_back(11); ... and would rather use a function "fillVector": vector<int> iv = fillVector(42,9,11); like...
1
by: Alex Vinokur | last post by:
------ foo.cpp ------ #include <vector> using namespace std; int main() { const vector<int> v1 (10); const vector<bool> v2 (10); &v1;
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
4
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
0
by: citystud | last post by:
does C# support vector< vector<int> > ?if not how can i use vector?
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.