我正在尝试为包含地图
的节点的二叉搜索树编写迭代器Node<Key, Value>* node;
我必须遍历一组这些并且我真的不知道如何返回迭代器。
bool operator!=(const iterator& rhs) const{ ....idk...}
iterator& operator++(){
第二个令人困惑,因为我不确定如何返回迭代器&amp;
提前致谢
答案 0 :(得分:0)
迭代器只是一个对象,它表示容器中的特定项(二进制搜索树)。当您递增迭代器(使用++)时,您移动迭代器以表示容器中的下一个项目。所以你的迭代器必须知道如何遍历容器中的下一个元素。
您的容器必须能够提供两个迭代器。
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).