Iterators for circular structures

后端 未结 2 2015
一个人的身影
一个人的身影 2021-01-06 16:52

The following code shows what I currently have. It is an adapter for circular data-structures. The main function shows how it is used. This is all nice and fast but I really

2条回答
  •  离开以前
    2021-01-06 17:47

    If it were me, I'd have operator++ notice the terminal condition, and set c to some sentinal value:

    circulator(circ& c) : c(&c), start(&c) {}
    circulator& operator++() { c = c->next; if(c==start) c=nullptr; return *this; }
    

    Usage:

    circulator begin{a}, end;
    while(begin != end) {
      begin++;
    }
    

    Note that this usage defines the end iterator as holding a nullptr, which means that you can't do this:

    circulator end;
    --end;
    

提交回复
热议问题