how to initialize list in a vector

B

Baby Lion

list<int> Curve;
vector<Curve> a;
a.reserve(10);

then how should I initialize the list in the vector before
a[0].push_back(123) ?

what about
list<int>Curve ;
vector<Curve *> a;
a.reserve(10);
 
G

Guest

list<int> Curve;
vector<Curve> a;
a.reserve(10);

then how should I initialize the list in the vector before
a[0].push_back(123) ?

what about
list<int>Curve ;
vector<Curve *> a;
a.reserve(10);

By passing an argument to the vector's constructor:

list<int> Curve;
vector<Curve> a(10);
a[0].push_back(123);
 
J

Joel Yliluoma

list<int> Curve;
vector<Curve> a;
a.reserve(10);

then how should I initialize the list in the vector before
a[0].push_back(123) ?

what about
list<int>Curve ;
vector<Curve *> a;
a.reserve(10);

I do not understand your question.

Are you perhaps asking, how to write this?
std::vector< std::list<int> > a;
Well, it is written like this.
 
A

Andrew Koenig

list<int> Curve;
vector<Curve> a;
a.reserve(10);
then how should I initialize the list in the vector before
a[0].push_back(123) ?

No need: When you executed

a.reserve(10);

you caused a to have 10 elements, each of which is a vector with no
elements. So when you call a[0].push_back, you cause a[0] to have one
element instead of the no elements it formerly had.

You could have saved yourself a little bit of trouble by writing

vector<Curve> a(10); // not a[10] !

instead of

what about
list<int>Curve ;
vector<Curve *> a;
a.reserve(10);

After you have executed this code, a will be a vector with 10 pointers, each
of which is 0. So you might say

a[0] = new list<int>;
a[0].push_back(123);

Of course, if you use this strategy, you will have to delete every list that
you allocated with new, otherwise your program will have a memory leak.
 
G

Guest

list<int> Curve;
vector<Curve> a;
a.reserve(10);
then how should I initialize the list in the vector before
a[0].push_back(123) ?

No need: When you executed

a.reserve(10);

you caused a to have 10 elements, each of which is a vector with no
elements. So when you call a[0].push_back, you cause a[0] to have one
element instead of the no elements it formerly had.

No, and I know that you know that this is wrong. reserve(n) only makes
sure that there is room enough in the vector (by re-allocating if
necessary) to add n elements without having to re-allocate. It does not
however change the number of elements in the vector, so in this case the
vector will still be empty and trying to access the first element will
result in undefined behaviour.
You could have saved yourself a little bit of trouble by writing

vector<Curve> a(10); // not a[10] !

Yes, that is the correct way to create a vector with 10 elements.

Replace the two lines above with
vector said:
After you have executed this code, a will be a vector with 10 pointers, each
of which is 0. So you might say

a[0] = new list<int>;
a[0].push_back(123);

Of course, if you use this strategy, you will have to delete every list that
you allocated with new, otherwise your program will have a memory leak.
 
R

red floyd

Andrew said:
list<int> Curve;
vector<Curve> a;
a.reserve(10);
then how should I initialize the list in the vector before
a[0].push_back(123) ?

No need: When you executed

a.reserve(10);

you caused a to have 10 elements, each of which is a vector with no
elements. So when you call a[0].push_back, you cause a[0] to have one
element instead of the no elements it formerly had.

Andrew, are you sure? I thought that reserve() merely allocated the
space, but the vector was still empty; whereas resize() allocates and
creates the elements.
 
D

duane hebert

red floyd said:
Andrew said:
list<int> Curve;
vector<Curve> a;
a.reserve(10);
then how should I initialize the list in the vector before
a[0].push_back(123) ?

No need: When you executed

a.reserve(10);

you caused a to have 10 elements, each of which is a vector with no
elements. So when you call a[0].push_back, you cause a[0] to have one
element instead of the no elements it formerly had.

Andrew, are you sure? I thought that reserve() merely allocated the
space, but the vector was still empty; whereas resize() allocates and
creates the elements.

I think it was a typo and he meant resize(10). Now I don't
feel so bad about that bug I found <g>
 
J

James Kanze

list<int> Curve;
vector<Curve> a;
a.reserve(10);
then how should I initialize the list in the vector before
a[0].push_back(123) ?
No need: When you executed

you caused a to have 10 elements, each of which is a vector with no
elements.

Now Andy, you know better than that. He needs a.resize(10) for
that.

Also, of course, his code shouldn't compile, since Curve is a
variable, not a type. He probably needs a typedef in the line
declaring Curve, but I'm not sure. I'm not at all sure what
he's really trying to do, but maybe something like:

typedef std::list< int > Curve ;
std::vector< Curve > a( 10 ) ;

is what he's looking for. Or maybe something more like:

typedef std::list< int > Curve ;
std::vector< Curve > a ;

if ( i >= a.size() ) {
a.resize( i + 1 ) ;
}
a[ i ].push_back( 42 ) ;

, if he doesn't know the actual size in advance.
 

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

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top