【发布时间】:2020-09-16 17:43:01
【问题描述】:
我正在尝试实现关于列表的列表和列表的迭代器,如 C++ STL。
列表中的节点定义如下:
struct Node{
Node *prev,*next;
value_type data;
};
我想重载运算符 > 和
bool list_iterator::operator>(const iterator_impl_base &rhs) const
bool list_iterator::operator<(const iterator_impl_base &rhs) const
这意味着如果我需要调用 next 来到达 rhs.node ,它将在 > 中返回 0 并在 <.> 中返回 1
如果我需要调用 prev 来到达 rhs.node ,它在 > 中返回 1,在 <.> 中返回 0
我使用循环列表实现列表。以下是列表类的一部分:
class List : public ordered_container {
protected:
Node* begin_;
Node* end_;
size_type size_;
public:
List::List() : end_(new Node){
begin_ = end_->prev = end_->next = end_;
size_=0;
}
}
所以,我不知道如何区分我是否只是通过了列表的begin_。有人可以帮我吗?谢谢。
【问题讨论】:
-
这个问题的上下文是什么?如果您只需要一个循环缓冲区,那么在索引时应用 mod 到向量的长度时向量是否足够?或者你可以使用
boost::circular_buffer吗? -
@TamaMcGlinn 我想知道如何重载这两个运算符。我需要做的是实现自己的列表和迭代器,其工作方式类似于 STL 中的列表。此外,我不能在 STL 中使用任何东西。
-
@willy:你说的是doubly linked list还是circular buffer?我在问,因为排序关系在循环缓冲区上没有意义。每个元素都在其他元素之前,它是一个圆圈......
-
boost 不在 STL 中。告诉你的老师你发现了一个很好的漏洞并使用
boost::circular_buffer -
@phlipsy 例如,我有一个列表: 1->2->3->4 和 4->1 因为它是 circule ,并且 1 是 begin_ , 4 是 end_ 。在这种情况下,1