完成迭代器--操作
This commit is contained in:
@@ -222,14 +222,14 @@ namespace TinySTL{
|
|||||||
private:
|
private:
|
||||||
const T *ptr_;
|
const T *ptr_;
|
||||||
cntrPtr container_;
|
cntrPtr container_;
|
||||||
stack<const T *> stack_;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>root<6F><74>ptr_<72>ĸ<EFBFBD><C4B8>ڵ<EFBFBD><DAB5><EFBFBD>·<EFBFBD><C2B7>
|
stack<const T *> parent_;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>root<6F><74>ptr_<72>ĸ<EFBFBD><C4B8>ڵ<EFBFBD><DAB5><EFBFBD>·<EFBFBD><C2B7>
|
||||||
std::set<const T *> visited_;
|
std::set<const T *> visited_;
|
||||||
public:
|
public:
|
||||||
bst_iter(const T *ptr, cntrPtr container)
|
bst_iter(const T *ptr, cntrPtr container)
|
||||||
:ptr_(ptr), container_(container){
|
:ptr_(ptr), container_(container){
|
||||||
auto temp = container_->root_;
|
auto temp = container_->root_;
|
||||||
while (temp &&ptr_ && temp != ptr_ && temp->data_ != ptr_->data_){
|
while (temp &&ptr_ && temp != ptr_ && temp->data_ != ptr_->data_){
|
||||||
stack_.push(temp);
|
parent_.push(temp);
|
||||||
if (temp->data_ < ptr_->data_)
|
if (temp->data_ < ptr_->data_)
|
||||||
temp = temp->right_;
|
temp = temp->right_;
|
||||||
else if (temp->data_ > ptr_->data_)
|
else if (temp->data_ > ptr_->data_)
|
||||||
@@ -253,18 +253,18 @@ namespace TinySTL{
|
|||||||
};//end of bst_iter
|
};//end of bst_iter
|
||||||
template<class T>
|
template<class T>
|
||||||
bst_iter<T>& bst_iter<T>::operator ++(){
|
bst_iter<T>& bst_iter<T>::operator ++(){
|
||||||
visited_.insert(ptr_);
|
visited_.insert(ptr_);//<2F><>Ϊ<EFBFBD>ѷ<EFBFBD><D1B7><EFBFBD>
|
||||||
if (ptr_->right_ != 0){
|
if (ptr_->right_ != 0){
|
||||||
stack_.push(ptr_);
|
parent_.push(ptr_);
|
||||||
ptr_ = ptr_->right_;
|
ptr_ = ptr_->right_;
|
||||||
while (ptr_ != 0 && ptr_->left_ != 0){
|
while (ptr_ != 0 && ptr_->left_ != 0){
|
||||||
stack_.push(ptr_);
|
parent_.push(ptr_);
|
||||||
ptr_ = ptr_->left_;
|
ptr_ = ptr_->left_;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
if (!stack_.empty() && visited_.count(stack_.top()) == 0){
|
if (!parent_.empty() && visited_.count(parent_.top()) == 0){//<2F><><EFBFBD>ڵ<EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>
|
||||||
ptr_ = stack_.top();
|
ptr_ = parent_.top();
|
||||||
stack_.pop();
|
parent_.pop();
|
||||||
}else{
|
}else{
|
||||||
ptr_ = 0;
|
ptr_ = 0;
|
||||||
}
|
}
|
||||||
@@ -278,6 +278,30 @@ namespace TinySTL{
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
template<class T>
|
template<class T>
|
||||||
|
bst_iter<T>& bst_iter<T>::operator --(){
|
||||||
|
visited_.erase(ptr_);//<2F><>Ϊδ<CEAA><CEB4><EFBFBD><EFBFBD>
|
||||||
|
if (ptr_->left_ != 0){
|
||||||
|
parent_.push(ptr_);
|
||||||
|
ptr_ = ptr_->left_;
|
||||||
|
while (ptr_ && ptr_->right_){
|
||||||
|
parent_.push(ptr_);
|
||||||
|
ptr_ = ptr_->right_;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
if (!parent_.empty() && visited_.count(parent_.top()) == 1){//<2F><><EFBFBD>ڵ<EFBFBD><DAB5>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
ptr_ = parent_.top();
|
||||||
|
parent_.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
bst_iter<T> bst_iter<T>::operator --(int){
|
||||||
|
auto res = *this;
|
||||||
|
--*this;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
bool operator ==(const bst_iter<T>& it1, const bst_iter<T>& it2){
|
bool operator ==(const bst_iter<T>& it1, const bst_iter<T>& it2){
|
||||||
return it1.ptr_ == it2.ptr_;
|
return it1.ptr_ == it2.ptr_;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user