我正在嘗試使用unique_ptr
實現BST。我有一個shared_ptr
的工作計劃。我該如何去使用unique_ptr來強制執行BinarySearchTree的單一所有權語義?在BST中使用unique_ptr代替shared_ptr
當我將shared_ptr
替換爲unique_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
{
spTreeNode temp = root;
spTreeNode prev = root;
while (temp)
{
prev = temp;
if (temp->data < ref)
temp = temp->right;
else
temp = temp->left;
}
if (prev->data < ref)
prev->right.reset(node);
else
prev->left.reset(node);
}
}
template<class T>
void BinarySearchTree<T>::print()const
{
print(root);
}
template<class T>
void BinarySearchTree<T>::print(const spTreeNode node)const
{
if (node==nullptr)
return;
print(node->left);
std::cout << node->data<< std::endl;
print(node->right);
}
int main()
{
BinarySearchTree<int> bst;
bst.insert(13);
bst.insert(3);
bst.insert(5);
bst.insert(31);
bst.print();
return 0;
}
編輯:如果有人感興趣編譯錯誤。警告:牆上的文字。
prog.cpp: In instantiation of ‘void BinarySearchTree<T>::insert(const T&) [with T = int]’:
prog.cpp:75:18: required from here
prog.cpp:39:27: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = BinarySearchTree<int>::TreeNode; _Dp = std::default_delete<BinarySearchTree<int>::TreeNode>]’
spTreeNode temp = root;
^
In file included from /usr/include/c++/4.8/memory:81:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/unique_ptr.h:273:7: error: declared here
unique_ptr(const unique_ptr&) = delete;
^
prog.cpp:40:27: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = BinarySearchTree<int>::TreeNode; _Dp = std::default_delete<BinarySearchTree<int>::TreeNode>]’
spTreeNode prev = root;
^
In file included from /usr/include/c++/4.8/memory:81:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/unique_ptr.h:273:7: error: declared here
unique_ptr(const unique_ptr&) = delete;
^
prog.cpp:43:18: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = BinarySearchTree<int>::TreeNode; _Dp = std::default_delete<BinarySearchTree<int>::TreeNode>]’
prev = temp;
^
In file included from /usr/include/c++/4.8/memory:81:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/unique_ptr.h:274:19: error: declared here
unique_ptr& operator=(const unique_ptr&) = delete;
^
prog.cpp:45:22: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = BinarySearchTree<int>::TreeNode; _Dp = std::default_delete<BinarySearchTree<int>::TreeNode>]’
temp = temp->right;
^
In file included from /usr/include/c++/4.8/memory:81:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/unique_ptr.h:274:19: error: declared here
unique_ptr& operator=(const unique_ptr&) = delete;
^
prog.cpp:47:22: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = BinarySearchTree<int>::TreeNode; _Dp = std::default_delete<BinarySearchTree<int>::TreeNode>]’
temp = temp->left;
^
In file included from /usr/include/c++/4.8/memory:81:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/unique_ptr.h:274:19: error: declared here
unique_ptr& operator=(const unique_ptr&) = delete;
^
prog.cpp: In instantiation of ‘void BinarySearchTree<T>::print() const [with T = int]’:
prog.cpp:79:15: required from here
prog.cpp:59:15: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = BinarySearchTree<int>::TreeNode; _Dp = std::default_delete<BinarySearchTree<int>::TreeNode>]’
print(root);
^
In file included from /usr/include/c++/4.8/memory:81:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/unique_ptr.h:273:7: error: declared here
unique_ptr(const unique_ptr&) = delete;
^
prog.cpp:63:6: error: initializing argument 1 of ‘void BinarySearchTree<T>::print(BinarySearchTree<T>::spTreeNode) const [with T = int; BinarySearchTree<T>::spTreeNode = std::unique_ptr<BinarySearchTree<int>::TreeNode, std::default_delete<BinarySearchTree<int>::TreeNode> >]’
void BinarySearchTree<T>::print(const spTreeNode node)const
^
也許張貼這些編譯錯誤將通過_our_理解方式幫助你。編輯:然而,你的一個函數需要一個值(即複製)的指針。但'unique_ptr's只能被移動。 – Quentin
@Quentin,我可以但他們很長。無論如何,我將發佈它們 –
遍歷樹時,使用'TreeNode *'(無所有權),而不是'unique_ptr'(獨佔所有權)。樹,而不是遍歷它的代碼,擁有所有權。 –