kjohansen
December 15th, 2008, 06:42 AM
This is related to my earlier thread http://ubuntuforums.org/showthread.php?t=1009636
Recap:
"Item Request" has an Id and a quantity and some other non important fields.
I am reading a file, some lines say to add an "Item Request" to a list some say to decrease the quantity of a a request with a specific Id and some say to delete a specific Id. If instead of deleting an item request I just set its quantity to 0 the rest of the logic of my code works, but the list gets REALLY big. If I use erase, I get the wrong results.
for(it=items.begin();it!=items.end();++it)
{
if(it->ID==orderID) //orderID is a var in this scope
{
if(it->size > decreseAmount) //decreaseAmount is a var in this scope
{
it->size-=decreaseAmount;
}
else
{
it->size-=decreaseAmount;
items.erase(it);
}
break;
}
}
I have tried several different ways to erase an entry. I found another forum with the suggestion to use remove which apparently moves the entries to the end of the list and then use erase on the end, I have tried just remove, but always the rest of it breaks.
I have written my own sorting routine that does not involve iterators only size and random access (i had to switch to vectors to do this).
I have checked the size before and after and it is correct. Is this the proper way to delete an entry in a vector or a list?
Edit: I forgot to mention that later on in the logic I am sorting the list using the built in sort command (list.sort()). I mention this because I dont know if somehow the erase is not updating the list.end() and it is somehow sorting the wrong thing, I dont know I have been looking at this for two days and cant find an error.
Recap:
"Item Request" has an Id and a quantity and some other non important fields.
I am reading a file, some lines say to add an "Item Request" to a list some say to decrease the quantity of a a request with a specific Id and some say to delete a specific Id. If instead of deleting an item request I just set its quantity to 0 the rest of the logic of my code works, but the list gets REALLY big. If I use erase, I get the wrong results.
for(it=items.begin();it!=items.end();++it)
{
if(it->ID==orderID) //orderID is a var in this scope
{
if(it->size > decreseAmount) //decreaseAmount is a var in this scope
{
it->size-=decreaseAmount;
}
else
{
it->size-=decreaseAmount;
items.erase(it);
}
break;
}
}
I have tried several different ways to erase an entry. I found another forum with the suggestion to use remove which apparently moves the entries to the end of the list and then use erase on the end, I have tried just remove, but always the rest of it breaks.
I have written my own sorting routine that does not involve iterators only size and random access (i had to switch to vectors to do this).
I have checked the size before and after and it is correct. Is this the proper way to delete an entry in a vector or a list?
Edit: I forgot to mention that later on in the logic I am sorting the list using the built in sort command (list.sort()). I mention this because I dont know if somehow the erase is not updating the list.end() and it is somehow sorting the wrong thing, I dont know I have been looking at this for two days and cant find an error.