清理 STL 指针列表/向量
您可以使用最短的 C++ 块来安全地清理 std::vector
或 std::list
指针? (假设您必须对指针调用删除?)
list<Foo*> foo_list;
我宁愿不使用 Boost 或用智能指针包装我的指针。
What is the shortest chunk of C++ you can come up with to safely clean up a std::vector
or std::list
of pointers? (assuming you have to call delete on the pointers?)
list<Foo*> foo_list;
I'd rather not use Boost or wrap my pointers with smart pointers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
对于
std::list
使用:对于
std::vector
使用:不知道为什么我采取
front
而不是上面的std::list
的back
。 我想就是感觉速度变快了。 但实际上两者都是恒定时间:)。 不管怎样,将它包装成一个函数并享受乐趣:For
std::list<T*>
use:For
std::vector<T*>
use:Not sure why i took
front
instead ofback
forstd::list
above. I guess it's the feeling that it's faster. But actually both are constant time :). Anyway wrap it into a function and have fun:既然我们在这里发出挑战...“C++ 的最短部分”,
我认为我们可以相信那些提出 STL 的人拥有高效的算法。 为什么要重新发明轮子?
Since we are throwing down the gauntlet here... "Shortest chunk of C++"
I think we can trust the folks who came up with STL to have efficient algorithms. Why reinvent the wheel?
如果你允许 C++11,你可以做 Douglas Leeder 答案的一个非常简短的版本:
If you allow C++11, you can do a very short version of Douglas Leeder's answer:
依赖容器外部的代码来删除指针确实很危险。 例如,当容器由于抛出异常而被销毁时会发生什么?
我知道你说过你不喜欢 boost,但请考虑 增强指针容器。
It's really dangerous to rely on code outside of the container to delete your pointers. What happens when the container is destroyed due to a thrown exception, for example?
I know you said you don't like boost, but please consider the boost pointer containers.
我不确定函子方法是否会在此处胜出。
不过,我通常会建议不要这样做。 一般来说,将指针包装在智能指针中或使用专门的指针容器会更加健壮。 有很多方法可以从列表中删除项目(各种类型的
erase
、clear
、销毁列表、通过迭代器分配到列表中等。 )。 你能保证把他们全部抓到吗?I'm not sure that the functor approach wins for brevity here.
I'd usually advise against this, though. Wrapping the pointers in smart pointers or using a specialist pointer container is, in general, going to be more robust. There are lots of ways that items can be removed from a list ( various flavours of
erase
,clear
, destruction of the list, assignment via an iterator into the list, etc. ). Can you guarantee to catch them all?当您的列表超出使用 RAII 的范围或调用 list::clear() 时,以下 hack 会删除指针。
例子:
The following hack deletes the pointers when your list goes out of scope using RAII or if you call list::clear().
Example:
至少对于一个列表来说,迭代和删除,然后在最后调用clear有点低效,因为它涉及遍历列表两次,而实际上你只需要执行一次。 这是一个更好的方法:
也就是说,您的编译器可能足够聪明,可以循环组合两者,具体取决于 list::clear 的实现方式。
At least for a list, iterating and deleting, then calling clear at the end is a bit inneficient since it involves traversing the list twice, when you really only have to do it once. Here is a little better way:
That said, your compiler may be smart enough to loop combine the two anyways, depending on how list::clear is implemented.
实际上,我相信 STD 库以 的形式提供了管理内存的直接方法allocator 类
您可以扩展基本分配器的 deallocate() 方法来自动删除任何容器的成员。
我/认为/这就是它的用途。
Actually, I believe the STD library provides a direct method of managing memory in the form of the allocator class
You can extend the basic allocator's deallocate() method to automatically delete the members of any container.
I /think/ this is the type of thing it's intended for.
您不想这样做有一个小原因 - 您实际上是在列表上迭代两次。
std::list<>::clear 的复杂度是线性的; 它在循环中一次删除并销毁一个元素。
考虑到上述内容,我认为最简单的阅读解决方案是:
There's a small reason why you would not want to do this - you're effectively iterating over the list twice.
std::list<>::clear is linear in complexity; it removes and destroys one element at a time within a loop.
Taking the above into consideration the simplest to read solution in my opinion is:
自 C++11 起:
或者,如果您正在编写模板化代码并希望避免指定具体类型:
其中(自 C++14 起)可以缩短为:
Since C++11:
Or, if you are writing templated code and want to avoid specifying a concrete type:
Which (since C++14) can be shortened as:
在我看来,这似乎是最干净的,但是你的 c++ 版本必须支持这种类型的迭代(我相信任何包含或在 c++0x 之前的内容都可以工作):
This seems cleanest imo, but your c++ version must support this type of iteration (I believe anything including or ahead of c++0x will work):