将标准::指针列表转换为标准::值列表的快速方法
Fast way to convert std::list of pointer to std::list of value
我有一个std::list<obj*>
,其中obj
是我的类:
std::list<obj*> list_of_ptr;
list_of_ptr.push_back(new obj());
我想将此列表转换为等效std::list<obj>
,之后我不再需要list_of_ptr
。
完成这项工作的最快方法是什么?
>std::transform
是你的朋友:
std::vector<obj> objects;
std::transform(
list_of_ptr.begin(), list_of_ptr.end(),
std::back_inserter(objects),
[](obj* p) { return *p; });
或者,如果无法使用 C++11 lambda 表达式,则可以使用简单的函数对象来执行间接寻址:
struct indirect
{
template <typename T>
T& operator()(T* p) { return *p; }
};
std::transform(
list_of_ptr.begin(), list_of_ptr.end(),
std::back_inserter(objects),
indirect());
或者,使用 boost::indirect_iterator
:
std::vector<obj> objects(
boost::make_indirect_iterator(list_of_ptr.begin()),
boost::make_indirect_iterator(list_of_ptr.end()));
当然,这些假设序列中没有空指针。 它留给读者一个练习,以弄清楚如何正确管理list_of_ptr
中的指针所指向的对象的生存期。
理想情况下,从一开始就使用std::vector<obj>
,或者,如果不可能,则使用智能指针的容器。 手动管理指向对象的生存期并正确执行此操作非常困难。 C++具有很棒的自动对象生存期管理工具(析构函数,智能指针,容器,堆栈语义,RAII),并且没有理由不使用它们。
简单易懂的代码也是你的朋友:
for each (obj* pObj in list_of_ptr)
{
if (pObj != nullptr)
{
list_of_objects.push_back(*pObj);
}
}
如果这不适合您,这当然应该:
std::list<obj> list_of_objects;
for_each(list_of_ptr.begin(), list_of_ptr.end(), [&list_of_objects] (obj* pObj) {
if (pObj != nullptr)
list_of_objects.push_back(*pObj);
});
相关文章:
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 在子集化后将包含索引号的列表列表映射到标准索引序列
- 移动后是否需要重置标准::列表?
- C++:带有大括号初始化列表的函数调用表达式 - 标准是否规定在单个元素列表的微不足道的情况下忽略大括号?
- C++,标准::列表的左/右旋转
- 标准::列表是循环的吗?
- 标准::p空气<矢量、双精度的初始值设定项列表<int>>
- 在地图和列表之间查找标准容器
- 标准::列表转换和复制
- 从标准列表中删除元素
- 如何在C++中访问标准::列表中的函数
- 擦除标准::列表项时的错误
- 标准::列表::擦除不起作用
- 两个标准::列表之间的距离
- C++ 自己在标准::列表中的比较
- 删除标准::列表中的重复项
- SFML 精灵标准::列表
- 当此列表包含父列表时,如何遍历列表,它使用标准列表
- 标准::列表擦除(可用内存)
- 从标准::列表中删除/擦除