Connecting Tech Pros Worldwide Forums | Help | Site Map

how to initialize list in a vector

Baby Lion
Guest
 
Posts: n/a
#1: Sep 26 '07
list<intCurve;
vector<Curvea;
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);


Baby Lion
Guest
 
Posts: n/a
#2: Sep 26 '07

re: how to initialize list in a vector


thanks~~

=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#3: Sep 26 '07

re: how to initialize list in a vector


On 2007-09-26 09:44, Baby Lion wrote:
Quote:
list<intCurve;
vector<Curvea;
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<intCurve;
vector<Curvea(10);
a[0].push_back(123);

--
Erik Wikström
Joel Yliluoma
Guest
 
Posts: n/a
#4: Sep 26 '07

re: how to initialize list in a vector


On Wed, 26 Sep 2007 00:44:44 -0700, Baby Lion wrote:
Quote:
list<intCurve;
vector<Curvea;
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.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Andrew Koenig
Guest
 
Posts: n/a
#5: Sep 26 '07

re: how to initialize list in a vector


"Baby Lion" <BabyLion.Liang@gmail.comwrote in message
news:1190792684.579511.266060@d55g2000hsg.googlegr oups.com...
Quote:
list<intCurve;
vector<Curvea;
a.reserve(10);
Quote:
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<Curvea(10); // not a[10] !

instead of

vector<Curvea;
a.reserve(10);
Quote:
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.


Baby Lion
Guest
 
Posts: n/a
#6: Sep 26 '07

re: how to initialize list in a vector


thanks~~

=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#7: Sep 26 '07

re: how to initialize list in a vector


On 2007-09-26 15:36, Andrew Koenig wrote:
Quote:
"Baby Lion" <BabyLion.Liang@gmail.comwrote in message
news:1190792684.579511.266060@d55g2000hsg.googlegr oups.com...
>
Quote:
>list<intCurve;
>vector<Curvea;
>a.reserve(10);
>
Quote:
>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.
Quote:
You could have saved yourself a little bit of trouble by writing
>
vector<Curvea(10); // not a[10] !
Yes, that is the correct way to create a vector with 10 elements.
Quote:
Quote:
>what about
>list<int>Curve ;
>vector<Curve *a;
>a.reserve(10);
Replace the two lines above with
vector<Curve*a(10);
Quote:
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.
--
Erik Wikström
red floyd
Guest
 
Posts: n/a
#8: Sep 26 '07

re: how to initialize list in a vector


Andrew Koenig wrote:
Quote:
"Baby Lion" <BabyLion.Liang@gmail.comwrote in message
news:1190792684.579511.266060@d55g2000hsg.googlegr oups.com...
>
Quote:
>list<intCurve;
>vector<Curvea;
>a.reserve(10);
>
Quote:
>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.


duane hebert
Guest
 
Posts: n/a
#9: Sep 26 '07

re: how to initialize list in a vector



"red floyd" <no.spam@here.dudewrote in message
news:CuuKi.924$yc5.111@nlpi069.nbdc.sbc.com...
Quote:
Andrew Koenig wrote:
Quote:
>"Baby Lion" <BabyLion.Liang@gmail.comwrote in message
>news:1190792684.579511.266060@d55g2000hsg.googleg roups.com...
>>
Quote:
>>list<intCurve;
>>vector<Curvea;
>>a.reserve(10);
>>
Quote:
>>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>


James Kanze
Guest
 
Posts: n/a
#10: Sep 27 '07

re: how to initialize list in a vector


On Sep 26, 3:36 pm, "Andrew Koenig" <a...@acm.orgwrote:
Quote:
"Baby Lion" <BabyLion.Li...@gmail.comwrote in message
Quote:
news:1190792684.579511.266060@d55g2000hsg.googlegr oups.com...
Quote:
Quote:
list<intCurve;
vector<Curvea;
a.reserve(10);
then how should I initialize the list in the vector before
a[0].push_back(123) ?
Quote:
No need: When you executed
Quote:
a.reserve(10);
Quote:
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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Closed Thread