繁体   English   中英

如何使用节点在C ++中为二进制搜索树编写迭代器

[英]How to write an iterator for a binary search tree in c++ using nodes

我正在尝试为包含由节点组成的二叉搜索树编写迭代器

Node<Key, Value>* node; 

我必须迭代其中的一组,但我真的不知道如何返回迭代器。

bool operator!=(const iterator& rhs) const{ ....idk...}

iterator& operator++(){

第二个令人困惑,因为我不确定如何返回迭代器&

提前致谢

迭代器只是一个对象,它表示容器(您的二进制搜索树)中的特定项目。 当增加迭代器(使用++)时,将移动迭代器以表示容器中的下一项。 因此,迭代器必须知道如何遍历容器中的下一个元素。

您的容器必须能够提供两个迭代器。

begin()   // returns an iterator to the first element.
end()     // returns an iterator to the one past the last element.
          // when an iterator is incremented passed the end of the container
          // it should compare equal to the iterator returned by this call.

您需要为最简单的迭代器之一(Forward Iterator)定义的操作是:

Node<Key, Value>&  operator*()     // Return a reference to the node
                                   // That the current iterator represents.

iterator&          operator++()    // Advance the iterator to the next element.
                                   // Return a reference to yourself.

iterator           operator++(int) // Advance the iterator. But return the
                                   // original value.

bool operator!=(iterator const & rhs) // Check if two iterators represent
                                      // the same node (or end). return true
                                      // if they do not (its not equal).

暂无
暂无

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

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