remove elements from an array
Hi!
I have this function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
{
int pos=-1;
for(int i=0;i<this->nrOfDiets;i++)
{
if(name==this->dh[i]->getUsrName()&&date==this->dh[i]->getDate())
{
delete this->dh[i];
this->dh[i]=this->dh[this->nrOfDiets-1];
this->nrOfDiets--;
pos=1;
}
}
return pos;
}
|
and if I have 5 elements in my array and want to remove them all I have to run the function 3 times :S
why? and how do I fix it?
best regards: Ogward
I assume right now some elements are not deleted because when you copy something from the end it gets skipped. Try
1 2 3 4 5
|
for(int i=0; ...; /*no increment here*/){
if(...){
delete ...
}else i++;
}
|
That way when you copy something from the end, it will be checked too.
It works, thanks a lot!
Topic archived. No new replies allowed.