Delete C++ structure from STL list using iterator

巧了我就是萌 提交于 2020-01-04 07:18:41

问题


I have this test program. I don't know how to delete struct in the list using iterator.

#include<iostream>
#include<list>
using namespace std;
typedef struct Node
{
    int * array;
    int id;
}Node;

void main()
{
    list<Node> nlist;
    for(int i=0;i<3;i++)
    {
        Node * p = new Node;//how to delete is later?

        p->array = new int[5];//new array
        memset(p->array,0,5*sizeof(int));

        p->id = i;

        nlist.push_back(*p);//push node into list
    }

    //delete each struct in list
    list<Node>::iterator lt = nlist.begin();
    while( lt != nlist.end())
    {
        delete [] lt->array;

        delete &(*lt);//how to delete the "Node"?

        lt++;
    }
}

I know how to delete the struct separately. It's like this:

Node * p = new Node;
p->array = new int[5];

delete [] p->array; //delete the array
delete p;//delete the struct

However, when it is pushed back into list, I don't know how to delete it according to the list iterator.

list<Node>::iterator lt = nlist.begin();
while( lt != nlist.end())
{
    delete [] lt->array;

    delete &(*lt);//how to delete the "Node"?

    lt++;
}

回答1:


Since you are declaring the list with list<Node> when you do:

nlist.push_back(*p)

it is actually creating a Node() and copying the data from the node you just dynamically allocated but not using the actual pointer. And then you try to delete a pointer from the object that the system will automatically delete:

delete &(*lt); // this causes double free

You need to declare the list like list<Node*> so that the pointer is inserted into the list. Although you should not really deal with this kind of allocation in c++, with a couple of modifications your code should work:

int main()
{
  list<Node*> nlist;
  for(int i=0;i<3;i++)
  {
    Node *p = new Node;//how to delete is later?

    p->array = new int[5];//new array
    memset(p->array,0,5*sizeof(int));

    p->id = i;

    nlist.push_back(p);//push node into list
  }

  //delete each struct in list
  list<Node*>::iterator lt = nlist.begin();
  while( lt != nlist.end())
  {
    delete [] (*lt)->array;

    delete *lt;//how to delete the "Node"?

    lt++;
  }

  return 0;
}



回答2:


You could use the list erase to delete a node from anywhere in between the list.

list<Node>::iterator it = nlist.begin();
advance(it,n); \\n is the node you want to delete, make sure its less than size of list
it = mylist.erase (it); 

Alternatively, if you want to delete elements from either ends of the list you can use the pop_back or the pop_front member functions.




回答3:


use list.erase But you are really doing that non-c++ way. You do not need allocate int[5] with new. Writing int[5] does what you want. Your Node type defined in c-way. In c++ you do not need to wrap it with typedef



来源:https://stackoverflow.com/questions/14018185/delete-c-structure-from-stl-list-using-iterator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!