简体   繁体   English

列表的元素,并迭代到下一个元素

[英]element of the list and iterator to the next element

I don't get what Bjarne has on mind stating: 我不明白Bjarne的想法:

A list iterator must be something more complicated than a simple pointer to an element because an element of a list in general does not know where the next element of that list is. 列表迭代器必须比指向元素的简单指针更复杂,因为列表的元素通常不知道该列表的下一个元素在哪里。 Thus, a list iterator might be a pointer to a link 因此,列表迭代器可能是指向链接的指针

I know that list's node has pointers to the next and previous nodes. 我知道列表的节点具有指向下一个和上一个节点的指针。 So how it "in general does not know"? 那么如何“一般不知道”呢?

For contrast, let's consider an iterator for std::vector . 相比之下,让我们考虑一下std::vector的迭代器。 Though you probably don't want to, you could basically implement it as a simple pointer: 虽然你可能不希望 ,你基本上可以实现它作为一个简单的指针:

template <class T>
class vector {
    T *data;
    size_t current_size;
    size_t alloc_size;
public:
    typedef T *iterator;
    iterator begin() { return data; }
    iterator end() { return data+current_size; }
    // ...
};

and since the data in the vector is contiguous, when you do (for example) ++ on the iterator, it would do the right thing (get you to the next item in the vector). 并且由于向量中的数据是连续的,因此当您在迭代器上执行++时,它将做正确的事情(将您带到向量中的下一项)。

With a linked list, it can't be that simple though -- if you used typedef to make iterator an alias for T * , it wouldn't work right. 对于链表,它并不是那么简单-如果您使用typedef来使迭代器成为T *的别名,那么它将无法正常工作。 When you tried to do ++ on the iterator, it would just increment the pointer, instead of taking you to the next element in the linked list like it needs to. 当您尝试在迭代器上执行++时,它只会增加指针,而不是像需要的那样将您带到链接列表中的下一个元素。

You need to build some intelligence into the iterator class so operator++ (for one example) knows to "chase" the pointer instead of just incrementing. 您需要在迭代器类中构建一些智能,以便operator ++(例如)知道“追逐”指针,而不仅仅是增加指针。

template <class T>
class list { 

    class node { 
        T data;
        node *next;
        node *prev;
    };
public:
    class iterator {
        node *pos;
    public:
        iterator operator++() {
            return pos = pos->next;
        }
        iterator operator--() {
            return pos = pos->prev;
        }
    };
};

Don't confuse list node with a list element. 不要将列表节点与列表元素混淆。 It might be a pointer, but to a node, which contains element and next/prev links. 可能是一个指针,但是指向一个包含元素和next / prev链接的节点。 If it were just a pointer to the element, you wouldn't be able to move around with it. 如果它只是指向该元素的指针,那么您将无法使用它。 (Though it's unlikely to be just pointer, since it typically has to overload operator++ to fetch next pointer instead of incrementing itself). (尽管它不太可能仅仅是指针,因为它通常必须重载operator ++才能获取下一个指针,而不是对其自身进行递增)。

This isn't true for intrusive lists, where node == element, but that's not std::list. 对于节点==元素的侵入式列表,情况并非如此,但这不是std :: list。

// illustrative
template <typename ElementType>
struct list_node {
    ElementType element;
    list_node *next, *prev;
};

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

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