[英]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.