Solved

removing a node from C++ list

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing the elements of a set declared inside a map in C++. 3 47
Header of docx file 17 102
Safe conversion? 4 67
Embarcadero C++ Builder XE10.1 Berlin red arrow Indicator 2 34
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

813 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

13 Experts available now in Live!

Get 1:1 Help Now