清理STL列表/指针向量 [英] Cleaning up an STL list/vector of pointers

查看:75
本文介绍了清理STL列表/指针向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++的最短块是什么,你可以想出来安全地清理一个向量或指针列表? (假设你必须在指针上调用delete)。

What is the shortest chunk of C++ you can come up with to safely clean up a vector or list of pointers? (assuming you have to call delete on the pointers?)

list<Foo*> foo_list;

我宁愿不使用Boost或用智能指针包装我的指针。

I'd rather not use Boost or wrap my pointers with smart pointers.

推荐答案

因为我们在这里扔了ga子...C ++的最短块

Since we are throwing down the gauntlet here... "Shortest chunk of C++"

static bool deleteAll( Foo * theElement ) { delete theElement; return true; }

foo_list . remove_if ( deleteAll );

我想我们可以信任那些提出STL的人有高效的算法。为什么要重新发明轮子?

I think we can trust the folks who came up with STL to have efficient algorithms. Why reinvent the wheel?

这篇关于清理STL列表/指针向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆