Link to home
Start Free TrialLog in
Avatar of sun307
sun307

asked on

removing a node from C++ list

Hi !
following is some code. the problem is with the remove() function coded below.
i want to remove the nodes found in the list which is pretty clear from the remove function, but removal is not working....
kindly help...

struct WinTable      {
      int id;
      HWND hNWnd;
};

typedef      list<WinTable> LT;

typedef list<WinTable>::iterator WI;

LT table;
//////////////////////////



void remove(int logicalID)
{
for(WI i=table.begin(); i!=table.end(); ++i)
{
if(i->id==logicalID)
    table.remove(i); //erroneous code
}
}



-Best regards
Sun307
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

list::remove() removes all elements that have are equal to the specified value.  i.e. it iterates through each item in the list and compares each item to the specified value, if they are the same, it deletes the item from the list.  (you could potentially be able to make the list::remove() function work for you by using an appropriate overloaded operator == function and a vey different algoritm in your remove() function.

list::erase() erases an element specified by an iterator, so this is the type of erasure/removal you are trying to do.

void remove(int logicalID)
{
   for(WI i=table.begin(); i!=table.end(); ++i)
   {
      if (i->id==logicalID)
        table.erase(i); //erroneous code
   }
}
Avatar of sun307

ASKER

Thanx Nietod !!
===============