Solved

removing a node from C++ list

Posted on 2000-05-01
3
178 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now