清理 STL 列表/指针向量

Cleaning up an STL list/vector of pointers(清理 STL 列表/指针向量)
本文介绍了清理 STL 列表/指针向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

为了安全地清理std::vectorstd::list 的指针,你能想到的最短的C++ 块是什么?(假设您必须在指针上调用 delete ?)

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;

我宁愿不使用 Boost 或用智能指针包裹我的指针.

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

推荐答案

既然我们要在这里挑战...最短的 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 列表/指针向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)