繁体   English   中英

二进制搜索树inorder iterator c ++

[英]binary search tree inorder iterator c++

我正在尝试为二叉搜索树实现迭代器。 我被要求不要在我的迭代器中使用任何STL。 我只需要覆盖运算符: ++operator*!= 我在operator*遇到错误:“no *operator匹配此操作数。操作数类型是*iterator<std::string> ”。 我正在使用模板库,所以我不确定它为什么不起作用。

这是我的代码:

template <typename T>
class Iterator : public std::iterator<std::forward_iterator_tag, {

public:
    Iterator(TreeNode<T>* root)
    {   
        this->current = root;
    }

    template <typename T>
    bool operator!=(Iterator<T> const & other) const
    {
        return this->current != other.current;
    }

    template <typename T>
    T &operator*() const {
        return current->element;
    }


    Iterator operator++()
    {
        current = current->nextInorder();
        return *this;
    }

    Iterator operator++(int dummy)
    {
        TreeNode<T> temp = current; 
        current = current->nextInorder();
        return *temp;
    }

private:
    TreeNode<T>* current;
    void nextInorder()
    {
        if (current->element == NULL)return;
        else {
            nextInorder(current->left);
            nextInorder(current->element);
            nextInorder(current->right);
        }
    }

};

代码没有很好地粘贴(参见class Iterator...行)。 我建议在bool operator!=(Iterator<T> const & other) const上删除template <typename T> bool operator!=(Iterator<T> const & other) constT &operator*() const方法。 因为T是用于类实例化的那个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM