Cleaning up an STL list/vector of pointers(清理 STL 列表/指针向量)
问题描述
为了安全地清理std::vector 或std::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 列表/指针向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:清理 STL 列表/指针向量
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
