How do i remove an element in the middle of a Vector? (without iterator usage)

Y

Yngve

Hi!

I have a (newbie) problem wich i would become glad if someone could help me
with.

I have a Vector of pointers to instances of another class. I would like to
remove a position in the middle of the vector. I am not using iterators at
the moment, but if i had i think that i could have used "erase".

This is my code now:
// ------------------------------

// Go through all the elements in my vector, increase iPos as we go
for (unsigned int iPos = 0 ; iPos < vPosList.size() ; iPos++) {
// Check if the position vPosList[iPos] is att coordinate iX,iY
if( vPosList[iPos]->equalPos(iX, iY) == true ) {
delete vPosList[iPos]; // Remove the object
// Here i would like to remove the element in the vector
// so that i don´t get a spot wich points to a non existing
// object --> crash..
}
}
 
J

John Harrison

Yngve said:
Hi!

I have a (newbie) problem wich i would become glad if someone could help me
with.

I have a Vector of pointers to instances of another class. I would like to
remove a position in the middle of the vector. I am not using iterators at
the moment, but if i had i think that i could have used "erase".
Exactly.


This is my code now:
// ------------------------------

// Go through all the elements in my vector, increase iPos as we go
for (unsigned int iPos = 0 ; iPos < vPosList.size() ; iPos++) {
// Check if the position vPosList[iPos] is att coordinate iX,iY
if( vPosList[iPos]->equalPos(iX, iY) == true ) {
delete vPosList[iPos]; // Remove the object
// Here i would like to remove the element in the vector
// so that i don´t get a spot wich points to a non existing
// object --> crash..

You need an iterator, fortunately since you are using a vector it's easy to
generate one from iPos.

vPosList.erase(vPosList.begin() + iPos);

john
 
J

John Harrison

Note that as written your code is probably bugged, since you will skip an
element in your loop when one is erased. You probably want to rewrite like
this

for (unsigned int iPos = 0 ; iPos < vPosList.size() ;) {
// Check if the position vPosList[iPos] is att coordinate iX,iY
if( vPosList[iPos]->equalPos(iX, iY) == true ) {
delete vPosList[iPos]; // Remove the object
vPosList.erase(vPosList.begin() + iPos);
} else {
++iPos;
}
}

Notice that iPos only gets incremented when you don't erase from the vector.

john
 
Y

Yngve

Note that as written your code is probably bugged, since you will skip an
element in your loop when one is erased. You probably want to rewrite

Thanks! I will get it working now. In my specific case that iPos incremental
problem isn´t a problem becouse immediatly after a delete i vill return and
get out of the method (removed it in the code for sake of clearity)..

Thanks!
 
Y

Yngve

Note that as written your code is probably bugged, since you will skip an
element in your loop when one is erased. You probably want to rewrite like
this

for (unsigned int iPos = 0 ; iPos < vPosList.size() ;) {
// Check if the position vPosList[iPos] is att coordinate iX,iY
if( vPosList[iPos]->equalPos(iX, iY) == true ) {
delete vPosList[iPos]; // Remove the object
vPosList.erase(vPosList.begin() + iPos);
} else {
++iPos;
}
}

Notice that iPos only gets incremented when you don't erase from the
vector.

I thought about this, and it works. But will the line:
vPosList.erase(vPosList.begin() + iPos);
Work in all cases? Or just when vPosList is a vector of pointers?
Or is vPosList.begin() something wich can be used as an iterator (in the
above case) and the last part (+ iPos) just tells the iterator
vPosList.begin() to point to the element iPos elements further? Hope you (or
someone else) understand what i meant.

Regars, Kent
 
J

John Harrison

I thought about this, and it works. But will the line:
vPosList.erase(vPosList.begin() + iPos);
Work in all cases?

All cases involving vectors (and more) but it wouldn't work with a list (for
instance).
Or just when vPosList is a vector of pointers?
Or is vPosList.begin() something wich can be used as an iterator (in the
above case)

vPosList.begin() is an iterator
and the last part (+ iPos) just tells the iterator
vPosList.begin() to point to the element iPos elements further?

That's right. Some iterators support + and some don't. The ones that do are
called random access iterators and any vector must have random access
iterators.
Hope you (or
someone else) understand what i meant.

Regars, Kent

john
 

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,792
Messages
2,569,639
Members
45,351
Latest member
RoxiePulli

Latest Threads

Top