使用智能指针的C ++链接列表 [英] C++ Linked list using smart pointers

查看:51
本文介绍了使用智能指针的C ++链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只对模板的链表使用了原始指针.例如,成员数据 Node< T> * head; ;当我插入节点时,其中一行将是 head = new Node< T>(data); .

I have only been using raw pointers for linked list with templates. For example, the member data, Node<T>* head; and when I am inserting a node one of the lines would be head = new Node<T>(data);.

但是,现在我需要使用智能指针,但是我不确定如何将其更改为使用智能指针.成员数据是否将更改为 shared_ptr< Node< T>>.head; ,另一行将更改为
head = shared_ptr< Node< T>>(新的< Node< T>>(data)); ?

However, now I need to use a smart pointer and I am not sure how I would change it to use smart pointers. Would the member data be changed to shared_ptr<Node<T>> head; and the other line would change to
head = shared_ptr<Node<T>>( new <Node<T>>(data) );?

推荐答案

您无需需要"为链接列表使用智能指针,因为该语句没有任何意义.您不会将智能指针用于底层数据结构.您将智能指针用于高级程序逻辑.

You do not "need" to use a smart pointer for a linked list, because that statement doesn't make sense. You do not use smart pointers for low-level data structures. You use smart pointers for high-level program logic.

就低级数据结构而言,您使用C ++标准库中的标准容器类,例如 std :: list [*] ,可以解决您所有的内存管理问题,而无需在内部使用任何智能指针.

As far as low-level data structures are concerned, you use a standard container class from the C++ standard library, like std::list [*], which solves all your memory-management problems anyway, without using any smart pointers internally.

如果您真的真的需要自己的高度专业化/优化的自定义容器类,因为整个C ++标准库均不符合您的要求,并且您需要替换用于 std :: list std :: vector std :: unordered_map 以及其他经过优化,测试,记录和安全的容器–我对此非常怀疑!&ndash ;,那么您还是必须手动管理内存,因为此类专门类的要点几乎肯定是需要内存池,写时复制甚至是垃圾回收等技术,而这些技术都与典型的智能冲突.指针的删除逻辑相当简单.

If you really really need your own highly specialised/optimised custom container class because the entire C++ standard library is unfit for your requirements and you need a replacement for std::list, std::vector, std::unordered_map and other optimised, tested, documented and safe containers – which I very much doubt! –, then you have to manage memory manually anyway, because the point of such a specialised class will almost certainly be the need for techniques like memory pools, copy-on-write or even garbage collection, all of which conflict with a typical smart pointer's rather simplistic deletion logic.

草药萨特:

从不使用拥有的原始指针并删除,除非在极少数情况下实施自己的低级数据结构(甚至保持很好地封装在类边界之内.

Never use owning raw pointers and delete, except in rare cases when implementing your own low-level data structure (and even then keep that well encapsulated inside a class boundary).

Herb Sutter和Bjarne Stroustrup的C ++核心指南:

无法通过转换所有所有权来大规模解决此问题指向unique_ptrs和shared_ptrs的指针,部分原因是我们需要/使用在实现中拥有原始指针"以及简单指针我们的基本资源.例如,通用向量实现具有一个拥有的指针和两个非拥有的指针.

This problem cannot be solved (at scale) by transforming all owning pointers to unique_ptrs and shared_ptrs, partly because we need/use owning "raw pointers" as well as simple pointers in the implementation of our fundamental resource handles. For example, common vector implementations have one owning pointer and two non-owning pointers.

在C ++中使用原始指针编写链接列表类可能是一个有用的 academic 练习.用智能指针在C ++中编写链接列表类是毫无意义的学术活动.在生产代码中使用这两种自制的东西几乎是自动错误的.

Writing a linked-list class in C++ with raw pointers can be a useful academic exercise. Writing a linked-list class in C++ with smart pointers is a pointless academic exercise. Using any of these two self-made things in production code is almost automatically wrong.

[*] 或者只是 std :: vector ,因为由于缓存的局限性,无论如何几乎总是更好的选择.

[*] Or just std::vector, because due to cache locality that will almost always be the better choice anyway.

这篇关于使用智能指针的C ++链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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