Sunday, July 12, 2009

C++ List class remove?

Hi I have a class defined as shown, I am using this in a list.


Within main I am adding in some data to the class, that works fine. I am trying to remove a value from the class as shown below the compiler doesn't like that.





OR How do I remove a value from the class?





#include "stdafx.h"


#using %26lt;mscorlib.dll%26gt;


#include %26lt;list%26gt;


#include %26lt;string%26gt;


#include %26lt;iostream%26gt;








using namespace std;





class person


{


public:


string Name;


int age;





public:


person () {


Name = "";


age = 0;


}





person(char *n,int a){





Name=n;


age = a;





}











void report() {





cout %26lt;%26lt; "Name: \n" %26lt;%26lt; Name;


cout %26lt;%26lt; "Age: \n" %26lt;%26lt; age;





}





};








void main()


{





list%26lt;person%26gt; pers;


pers.push_back(person("Siva", 35));


pers.push_back(person("Ram", 30));


pers.push_back(person("SS", 30));


pers.push_back(person("RR", 30));


while(p != pers.end()) {


p-%26gt;report();


p++;


}





pers.remove("Siva");

C++ List class remove?
The problem is finding the proper instance in the list. The function list::remove expects an object of the same type as stored in the list. You have a list of person, but you pass remove a null terminated character string.





To use remove, you need to construct a person object with the exact same values:


pers.remove(person("Siva", 35));





What you are trying to do is to look for a person instance that happens to have a particular name, irrespective of the age. To do this, you have to write a comparison object that takes a person and compares the person's name with the target name. Then use this comparison object in std::find





class NameSearch {


public:


NameSearch(string target) : m_target(target) {}


bool operator () (person const %26amp; p)


{ return person.name == m_target }


private:


string m_target;


};





NameSearch ns("Siva");


list%26lt;person%26gt;::iterator it;


it = std::find(pers.begin(), pers.end(), ns);


if (it != pers.end()) // check if "Siva" is found in list


pers.erase(it);





Read up on iterators, they are necessary for using standard collection classes and algorithms. Once you understand them you'll see how nicely the algorithms and collections work together.
Reply:I would suspect you need to supply a person instance detailing which element you wish to remove.
Reply:check the syntax for the remove function of the list.





what i think is that the pointer (p) is pointing to the end of the list after the while loop terminates. then since you try to remove it, it does not exist.





i do not know how the remove is actually defined so im only making a guess as to what happened. if the remove does not search the list for the specific item then this is what happened.





however, if the list searches for the item then deletes it, then i don't know what happened :-)





find out how the remove is actually defined then you will know the problem





i personally just make my own list and not rely on predefined lists.
Reply:yeah WHAT?! makes nooooooooooooooooooooooo since to me................


No comments:

Post a Comment