Jump to content
  • Advertisement
Sign in to follow this  
rom7L

removing STL list objects

This topic is 5404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have a list of pointers to node structures (called nList) - each node contains a list of pointers to other nodes. i'm trying to write a function to delete a node (which will hopefully delete the list belonging to the node as well). i have tried using a list iterator to search through the list to find the node, then use nList.erase(iterator) remove it. this doesn't seem to have any effect though. here's my function:
void nodeList::removeNode()
{
       string xnode;
       node* xnodePtr = NULL;
       cout << endl << "Enter name of node to remove: ";
       cin >> xnode;
			
       typedef list<node>::iterator ListIterator;
       for (ListIterator l = nList.begin(); l != nList.end();l++) 
	{
	   if ( stricmp( l->name.c_str(), xnode.c_str()) == 0 )
	   {
	      nList.erase(l);  
           }
        }
}

Share this post


Link to post
Share on other sites
Advertisement
If you want to keep your old code you need to change

nList.erase(l);

to

l = nList.erase( l );


Since after erasing the current and (with a list) the following iterators are invalid you should use the iterator returned by erase (which points to the next element or the end of the list).

Share this post


Link to post
Share on other sites
Quote:
Original post by rom7L

if ( stricmp( l->name.c_str(), xnode.c_str()) == 0 )



this is redudndant and not a good idea, std::string already has an overloaded equailty operator to compare C++ strings & C-style strings. Also writing explicit loops to search & remove an element is redudndant there STL algorithms that let preform that.

Anyways you can use this:


#include <functional>
#include <algorithm>

struct node_equals : std::unary_function< const node&, bool > {

const std::string key;

node_equals(const std::string& s): key(s) {}

bool operator()(const node& n) const {
return n.name == key;
}

};

void nodeList::removeNode() {
string xnode;
cout << endl << "Enter name of node to remove: ";
cin >> xnode;

nList.erase(std::remove_if(nList.begin(), nList.end(), node_equals(xnode)), nList.end());

}


I recommend before you write routine with STL containers check if there already is an algorithm that exist, you can have a look here

Share this post


Link to post
Share on other sites
thnx again snk

this works - it does remove the node, but it crashes after it does so. it seems to only crash if it tries to delete a node that has an associated edge list (not all the nodes have edges). this is my node struct:


struct node
{
string name;
list<node*> edgeList;
node(string nodeName)
{
name = nodeName;
}
};


it works fine if the node has no edgeList. maybe i missunderstood you - should i add your node_equals struct to my node struct?

Share this post


Link to post
Share on other sites
ok, update: it crashes when it tries to remove a node that was part of the original graph that was read from the file. (after the original graph is created, the user can add/remove nodes). i'm able to remove nodes and edgelists that were added by the user ... just not the ones that were created from the file. hmmm.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!