Solved

removing a node from C++ list

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 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