C++ 如何删除集合中的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5838922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete an object in a set
提问by node ninja
In my C++ program, I create objects in one function using new. These objects are inserted into a set. When I want to remove objects from the set, I use an iterator in a for-loop. When I remove the object from the set, I still need to delete the object to free its memory, correct? I tried using delete, but then I get an error saying that the pointer being freed was not allocated. So how can this be done?
在我的 C++ 程序中,我使用 new 在一个函数中创建对象。这些对象被插入到一个集合中。当我想从集合中删除对象时,我在 for 循环中使用迭代器。当我从集合中删除对象时,我仍然需要删除对象以释放其内存,对吗?我尝试使用删除,但后来我收到一个错误,说被释放的指针没有被分配。那么如何做到这一点呢?
Here is the code where I create the object and then insert it into the set
这是我创建对象然后将其插入到集合中的代码
set <myObject> myobjectlist;
myObject *myobject = new myObject;
myobjectlist.insert(*myobject);
In another function, I try to remove an object from the set, and free its memory:
在另一个函数中,我尝试从集合中删除一个对象,并释放其内存:
for (set<myObject>::iterator i = myobjectlist.begin(); i != myobjectlist.end(); i++)
if (i->myObjectID == myObjectID)
{
myobjectlist.erase(*i);
delete &i;
break;
}
This works fine without the 'delete' part. I added it in because I thought that the memory from the object wasn't being freed.
这在没有“删除”部分的情况下工作正常。我添加它是因为我认为对象的内存没有被释放。
采纳答案by Ben Stott
Assuming you're calling the set's erase()
method, note that this will call the destructor of the object for you. After you erase()
your object, it has already been delete
d, and thus your second attempt to manually call delete will fail as the pointer is no longer allocated.
假设您正在调用 set 的erase()
方法,请注意这将为您调用对象的析构函数。在erase()
您创建对象之后,它已经是delete
d,因此您第二次手动调用 delete 的尝试将失败,因为不再分配指针。
For reference, see this
作为参考,请参阅此
回答by hifier
Here is what you want, assuming that you need to use new to allocate these objects:
这是您想要的,假设您需要使用 new 来分配这些对象:
set <myObject*> myobjectlist;
myObject *myobject = new myObject;
myobjectlist.insert(myobject); //insert the pointer, not the object
for (set<myObject*>::iterator i = myobjectlist.begin(); i != myobjectlist.end(); i++) {
if ((*i)->myObjectID == myObjectID) {
myobjectlist.erase(i);
delete *i;
break;
}
}
回答by sean e
Yes, you need to delete objects that you create. However, what is in your set isn't necessarily what you allocated. For example, maybe your set contains object values (rather than pointers) and your allocated object is being leaked after insert. Post code.
是的,您需要删除您创建的对象。但是,您的集合中的内容不一定是您分配的内容。例如,您的集合可能包含对象值(而不是指针),并且您分配的对象在插入后被泄漏。邮政编码。
Edit: That was it. Your set does not store pointers, it stores copies of the objects you are allocating. Remove the delete from your erase loop and insert the object like this:
编辑:就是这样。您的集合不存储指针,它存储您正在分配的对象的副本。从擦除循环中删除删除并像这样插入对象:
set <myObject> myobjectlist;
myobjectlist.insert(myObject());
Alternatively, just make your set be set<myObject*>
.
或者,只需将您的集合设为set<myObject*>
.
Also, erase takes an iterator - no need to deref it.
此外,擦除需要一个迭代器 - 无需取消引用它。
回答by TimW
If you need a list of pointers, use a list of smart pointers. Use a std algoritm to find the correct item and erase it from the list.
如果您需要指针列表,请使用智能指针列表。使用标准算法找到正确的项目并将其从列表中删除。
#include <set>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
using namespace boost;
typedef boost::shared_ptr<MyObject> t_object;
std::set<t_object> myObjectList;
myObjectList.insert(t_object(new MyObject));
std::set<t_object>::iterator item = std::find_if(
myObjectList.begin(),
myObjectList.end(),
bind(&MyObject::myObjectID, _1)== myObjectID);
if(item!=myObjectList.end())
myObjectList.erase(item);