Connecting Tech Pros Worldwide Help | Site Map

how to initialize list in a vector

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 26th, 2007, 07:45 AM
Baby Lion
Guest
 
Posts: n/a
Default how to initialize list in a vector

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);


  #2  
Old September 26th, 2007, 07:55 AM
Baby Lion
Guest
 
Posts: n/a
Default Re: how to initialize list in a vector

thanks~~

  #3  
Old September 26th, 2007, 09:35 AM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
Default 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
  #4  
Old September 26th, 2007, 09:45 AM
Joel Yliluoma
Guest
 
Posts: n/a
Default 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)
  #5  
Old September 26th, 2007, 01:45 PM
Andrew Koenig
Guest
 
Posts: n/a
Default 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.


  #6  
Old September 26th, 2007, 02:15 PM
Baby Lion
Guest
 
Posts: n/a
Default Re: how to initialize list in a vector

thanks~~

  #7  
Old September 26th, 2007, 03:05 PM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
Default 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
  #8  
Old September 26th, 2007, 03:05 PM
red floyd
Guest
 
Posts: n/a
Default 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.


  #9  
Old September 26th, 2007, 03:25 PM
duane hebert
Guest
 
Posts: n/a
Default 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>


  #10  
Old September 27th, 2007, 09:35 AM
James Kanze
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.