c++ - How to erase an object of a list containing pointers to a struct? -


i have struct called node 3 members, 1 of acts id objects:

struct node {   string mem1;   string mem2;   int id; } 

and list containing pointers node objects:

list<node*> g_node; 

the problem comes when trying erase specific object list (localized id). have code doesn't work:

list<node>::iterator = g_node.begin();             while (it != g_node.end()){                 if (it->id == iden)                 {                     g_node->erase(it);                 }             }         } else if (iden != 0) { 

"iden" id of object deleted, , input user.

what's going wrong?

remove_if great idea, if want have function can reuse , customize @ will, can this:

bool remove_from_list(int id, list<node*> &g_node) {     auto = g_node.begin();      while (it != g_node.end())     {         if ((*it)->id == id)         {             // free memory... if allocated pointers             delete (*it);              g_node.erase(it);             return true;         }         else             it++;     }      return false; }  list<node*> g_node; g_node.push_back(new node { "a", "b", 5 }); g_node.push_back(new node { "ee", "77", 6 }); remove_from_list(5, g_node); 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -