By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Starting to use STL containers...

P: n/a
I finally decided to start using those, I think I have a good enough idea of
how they work, but I still have a few questions

First, is an iterator really a pointer to an item, I mean, will this work :

std::list <CObject> ObjectList;

CObject* NewObject()
{
ObjectList.push_back( CObject());
return (CObject*) (ChanList.end() - 1;
}

I saw that vectors use adjacent memory, meaning that if I have a pointer to
one of its elements, if a previous element gets erased it won't be pointing
to the good element anymore (possibly even points to garbage)
I'm using list because I think I read it doesnt do that, but I would find
the find() member of map useful, I supposes map works more like list but I
wanted a confirmation.

Thanks for answering and poining me out if I understood something wrong.
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Flzw" <fl****@wanadoo.fr> wrote:
I finally decided to start using those, I think I have a good enough idea of
how they work, but I still have a few questions

First, is an iterator really a pointer to an item, I mean, will this work :
No. Although vector::iterator could be implemented with one.
std::list <CObject> ObjectList;

CObject* NewObject()
{
ObjectList.push_back( CObject());
return (CObject*) (ChanList.end() - 1;
}
No (the above won't even compile.) Try this instead:

CObject* NewObject() {
ObjectList.resize( ObjectList.size() + 1 );
return &ObjectList.back();
}
I saw that vectors use adjacent memory, meaning that if I have a pointer to
one of its elements, if a previous element gets erased it won't be pointing
to the good element anymore (possibly even points to garbage)
I'm using list because I think I read it doesnt do that, but I would find
the find() member of map useful, I supposes map works more like list but I
wanted a confirmation.


map works like a tree, list works like a double-link list, vector works
like an array.
Jul 22 '05 #2

P: n/a
First, is an iterator really a pointer to an item, I mean, will this
work :
No. Although vector::iterator could be implemented with one.
Ok

CObject* NewObject() {
ObjectList.resize( ObjectList.size() + 1 );
return &ObjectList.back();
}
I'm not sure this does what I want, I want to add an item to the list (which
implies creating the object, executing the constructor, which I'm not sure
your code does) and then return a pointer to this object.
does &it return the adress of it value ? or is it redifined to return the
adress of the object referred to by it ?
map works like a tree, list works like a double-link list, vector works
like an array.


I suppose this means objects are not moved when previous items are deleted.
Jul 22 '05 #3

P: n/a
On Sat, 14 Aug 2004 13:38:25 +0200, Flzw <fl****@wanadoo.fr> wrote:
> First, is an iterator really a pointer to an item, I mean, will this

work :

No. Although vector::iterator could be implemented with one.


Ok

CObject* NewObject() {
ObjectList.resize( ObjectList.size() + 1 );
return &ObjectList.back();
}


I'm not sure this does what I want, I want to add an item to the list
(which
implies creating the object, executing the constructor, which I'm not
sure
your code does) and then return a pointer to this object.
does &it return the adress of it value ? or is it redifined to return the
adress of the object referred to by it ?


The code does exactly what you want it to. There's no way to add an item
to a list (or anything else) without executing a constructor somewhere. I
don't understand your second objection, but back() return a reference to
the last element in a list and & turns that into a pointer.
But I would prefer

CObject* NewObject()
{
ObjectList.push_back( CObject() );
return &ObjectList.back();
}

I think its a little clearer.

john
Jul 22 '05 #4

P: n/a
The code does exactly what you want it to. There's no way to add an item
to a list (or anything else) without executing a constructor somewhere. I
don't understand your second objection, but back() return a reference to
the last element in a list and & turns that into a pointer.
yeah, I had end() in mind, back returns a reference ok.
But I would prefer

CObject* NewObject()
{
ObjectList.push_back( CObject() );
return &ObjectList.back();
}

I think its a little clearer.


It also lets you specify constructor arguments...

Thanks for your help
Jul 22 '05 #5

P: n/a
"Flzw" <fl****@wanadoo.fr> wrote in message news:<cf**********@news-reader1.wanadoo.fr>...
I finally decided to start using those, I think I have a good enough idea of
how they work, but I still have a few questions

First, is an iterator really a pointer to an item,
No, an iterator is an object. If it appears to be a pointer, that is
an implementation detail.
I mean, will this work :

std::list <CObject> ObjectList;

CObject* NewObject()
{
ObjectList.push_back( CObject());
return (CObject*) (ChanList.end() - 1;
You want to 'return ChanList.back();' here, so your question is moot.
}

I saw that vectors use adjacent memory, meaning that if I have a pointer to
one of its elements, if a previous element gets erased it won't be pointing
to the good element anymore (possibly even points to garbage)
True.
I'm using list because I think I read it doesnt do that, but I would find
the find() member of map useful, I supposes map works more like list but I
wanted a confirmation.


'map' may be implemented as a tree rather than a list. In any case,
you should choose a container based on how you will use it (e.g, need
to sort, heap property, many insertions and few deletions, insert at
front versus insert at back, many queries, random access, etc). The
performance guarantees of each container are documented.

HTH, /david
Jul 22 '05 #6

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote:
Flzw <fl****@wanadoo.fr> wrote:
> First, is an iterator really a pointer to an item, I mean, will this

work :

No. Although vector::iterator could be implemented with one.


Ok

CObject* NewObject() {
ObjectList.resize( ObjectList.size() + 1 );
return &ObjectList.back();
}


I'm not sure this does what I want, I want to add an item to the list
(which
implies creating the object, executing the constructor, which I'm not
sure
your code does) and then return a pointer to this object.
does &it return the adress of it value ? or is it redifined to return the
adress of the object referred to by it ?


The code does exactly what you want it to. There's no way to add an item
to a list (or anything else) without executing a constructor somewhere. I
don't understand your second objection, but back() return a reference to
the last element in a list and & turns that into a pointer.
But I would prefer

CObject* NewObject()
{
ObjectList.push_back( CObject() );
return &ObjectList.back();
}

I think its a little clearer.


Unfortunatly, it creates an extra CObject().
Jul 22 '05 #7

P: n/a
"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news02.east. earthlink.net...
<snip>
> CObject* NewObject() {
> ObjectList.resize( ObjectList.size() + 1 );
> return &ObjectList.back();
> }

I'm not sure this does what I want, I want to add an item to the list
(which
implies creating the object, executing the constructor, which I'm not
sure
your code does) and then return a pointer to this object.
does &it return the adress of it value ? or is it redifined to return the adress of the object referred to by it ?


The code does exactly what you want it to. There's no way to add an item
to a list (or anything else) without executing a constructor somewhere. I don't understand your second objection, but back() return a reference to
the last element in a list and & turns that into a pointer.
But I would prefer

CObject* NewObject()
{
ObjectList.push_back( CObject() );
return &ObjectList.back();
}

I think its a little clearer.


Unfortunatly, it creates an extra CObject().


How so? This is resize():

void resize(size_type sz, T c = T());

--
David Hilsee
Jul 22 '05 #8

P: n/a
In article <sP********************@comcast.com>,
"David Hilsee" <da*************@yahoo.com> wrote:
"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news02.east. earthlink.net...
<snip>
>> CObject* NewObject() {
>> ObjectList.resize( ObjectList.size() + 1 );
>> return &ObjectList.back();
>> }
>
> I'm not sure this does what I want, I want to add an item to the list
> (which
> implies creating the object, executing the constructor, which I'm not
> sure
> your code does) and then return a pointer to this object.
> does &it return the adress of it value ? or is it redifined to return the > adress of the object referred to by it ?

The code does exactly what you want it to. There's no way to add an item
to a list (or anything else) without executing a constructor somewhere. I don't understand your second objection, but back() return a reference to
the last element in a list and & turns that into a pointer.
But I would prefer

CObject* NewObject()
{
ObjectList.push_back( CObject() );
return &ObjectList.back();
}

I think its a little clearer.


Unfortunatly, it creates an extra CObject().


How so? This is resize():

void resize(size_type sz, T c = T());


Ouch, my mistake...
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.