Solved

removing a node from C++ list

Posted on 2000-05-01
3
209 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:sun307
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2765515
You want to use erase(), not remove().

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2765517
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
   }
}
0
 
LVL 2

Author Comment

by:sun307
ID: 2768117
Thanx Nietod !!
===============
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question