Starting to use STL containers...

F

Flzw

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.
 
D

Daniel T.

Flzw said:
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.
 
F

Flzw

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.
 
J

John Harrison

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
 
F

Flzw

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
 
D

David Rubin

Flzw said:
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
 
D

Daniel T.

John Harrison said:
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().
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top