相关疑难解决方法(0)

在BST中使用unique_ptr而不是shared_ptr

我正在尝试实施BST unique_ptr.我有一份工作计划shared_ptr.我如何使用unique_ptr来强制实施BinarySearchTree的单一所有权语义?

当我替换shared_ptrunique_ptr,我得到的编译错误超出了我的理解范围.

#include <iostream>
#include <memory>

template<class T>
class BinarySearchTree{
    struct TreeNode;
    typedef std::shared_ptr<TreeNode> spTreeNode;
    struct TreeNode{
        T data;
        spTreeNode  left;
        spTreeNode  right;
        TreeNode(const T & value):data(value),left(nullptr),right(nullptr){}
    };



    spTreeNode root;
    bool insert(spTreeNode node);
    void print(const spTreeNode) const ;
public:
    BinarySearchTree();
    void insert( const T & node);
    void print()const;
};

template<class T>
BinarySearchTree<T>::BinarySearchTree():root(nullptr){}

template<class T>
void BinarySearchTree<T>::insert(const T & ref)
{
    TreeNode *node = new TreeNode(ref);
    if (root==nullptr)
    {
        root.reset(node);
    }
    else
    { …
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr unique-ptr binary-search-tree c++11

4
推荐指数
1
解决办法
5249
查看次数

标签 统计

binary-search-tree ×1

c++ ×1

c++11 ×1

shared-ptr ×1

unique-ptr ×1