欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

C++11 智能指针unique_ptr使用 -- 以排序二叉树为例

发布时间:2025/7/25 c/c++ 61 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C++11 智能指针unique_ptr使用 -- 以排序二叉树为例 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

http://www.cnblogs.com/fstang/p/3947552.html

 

  用智能指针可以简化内存管理。以树为例,如果用普通指针,通常是在插入新节点时用new,在析构函数中调用delete;但有了unique_ptr类型的智能指针,就不需要在析构函数中delete了,因为当unique_ptr类型的指针P生命结束时(比如对于局部变量,程序执行到局部变量的作用域范围之外),P会自动delete它拥有的资源(指针指向的空间)。对于shared_ptr,情况更加复杂一些,shared_ptr会维护一个use count,即有多少个指针共享这一资源,当use count为0时,资源被自动释放。

    有一篇wiki专门解释了这种模式(将资源的获取与释放与对象的生命周期绑定),http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

    这篇文章主要关注unique_ptr,如果不熟悉,可以先读一下这个:http://www.cplusplus.com/reference/memory/unique_ptr/

    这里用排序二叉树的简单实现来展示如何使用unique_ptr,我们只实现插入一个int,以及中序遍历输出所有数字的功能,这已经足以解释unique_ptr的使用方法(其实是,我不太会写二叉树的平衡- -)

    TreeNode代表一个树节点,包括一个int值,和指向左右子树的智能指针。我们在TreeNode和BST里都实现了insert(int)和inOrder(),BST中的方法基本上是调用TreeNode的对应方法(BST的方法只是一个wrapper,真正干活的是TreeNode里的对应方法)

#include <cstdio> #include <iostream> #include <sstream> #include <string> #include <memory> using namespace std; class TreeNode{public:unique_ptr<TreeNode> left;unique_ptr<TreeNode> right;int val;TreeNode(){}TreeNode(int value): val(value){}void insert(int value){if (value <= val) {if (left) {left->insert(value);} else {left.reset(new TreeNode(value));}} else {if (right) {right->insert(value);} else {right.reset(new TreeNode(value));}}}void inOrder(stringstream& ss){if (left){left->inOrder(ss);}ss << val << " ";if (right) {right->inOrder(ss);}} }; class BST { public:BST ();virtual ~BST ();void insert(int value);string inOrder(); private:unique_ptr<TreeNode> root; }; BST::BST(){} BST::~BST(){} void BST::insert(int value){if(root){root->insert(value);} else {root.reset(new TreeNode(value));} } string BST::inOrder(){if (root) {stringstream ss;root->inOrder(ss);return ss.str();} else {return "";}} int main(int argc, const char *argv[]) {BST bst;bst.insert(4);bst.insert(5);bst.insert(2);bst.insert(1);cout << bst.inOrder() << endl;return 0; }

     有人提到可能要把TreeNode指定为NonCopyable,见http://stackoverflow.com/questions/6679482/smart-pointers-for-modelling-a-general-tree-structure-its-iterators,挺有道理的,这个实现不算复杂,可以参考http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-copyable_Mixin

 

转载于:https://www.cnblogs.com/virusolf/p/5008651.html

总结

以上是生活随笔为你收集整理的C++11 智能指针unique_ptr使用 -- 以排序二叉树为例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。