STL算法删除容器中的所有对象?
是否有 STL 实用程序/算法可以对所有对象执行 delete *the_object_iterator;
操作?这样我就可以安全地 clear()
了? STL 容器是一个集合
,对象是指向使用new
创建的C++ 类的指针。
Boost 似乎是最好的解决方案。我的目标是避免对不可复制的类进行复制构造。
Is there a STL utility/algorithm to do delete *the_object_iterator;
on all the objects? So that I can clear()
safely? The STL container is a set
and the objects are pointers to C++ classes created with new
.
Boost seems to be the best solution. My goal was to avoid copy-construction on noncopyable classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用智能指针来保存类指针
Use a smart pointer to hold the class pointers
据我所知,没有标准的算法来删除所有对象。但是,您可以轻松构建一个:
As far as I know, there is no standard algorithm to delete all objects. However, you can build up one easily:
Boost 指针容器是正确的选择。
它们不仅存储动态分配的对象。但是这些对象可以作为引用进行访问,这使得在对象上使用标准算法变得更加容易。
Boost pointer containers are the way to go.
Not only do they store the dynamically allocated objects. But the objects are accessible as references which makes using the standard algorithms on the the object that much easier.