最简单的方法来做循环迭代器(循环器)? [英] Easiest way to make a cyclic iterator (circulator)?

查看:124
本文介绍了最简单的方法来做循环迭代器(循环器)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,我想在游戏中连续循环。我在 std :: vector 中有一系列坐标,我想用作航点。

I have an object that I want to travel in a continuous loop in a game. I have a series of coordinates in a std::vector that I want to use as waypoints.

任何方式使 std :: vector< T> :: iterator 循环(也称为循环器)?

Is there any way to make an std::vector<T>::iterator cyclic (also known as a circulator)?

最好的我可以想出是有两个迭代器,然后每当第一个迭代器耗尽分配给它的值第二(这不会用于做任何其他),但我甚至不确定它会工作 - 赋值运算符会复制迭代器用于保存索引的任何内容,否则它只会被引用(因此在第二轮之后将无用)?

The best I can come up with is to have two iterators and then whenever the first iterator is exhausted assign to it the value of the second (which would not be used to do anything else) but I am not even sure it will work - will the assignment operator copy whatever the iterator is using to hold the index or will it merely be referenced (and therefore will be useless after the second round)?

我想让对象永远移动航点(除非它被销毁,但不会发生在该方法中),但迭代器只会为每个框架,必须返回,以便我可以更新游戏中的其他对象。

I want the object to travel the waypoint forever (unless it is destroyed but that doesn't happen in that method), but the iterator will only be called once for each frame and must return so that I can update the other objects in the game.

解决方案必须工作在gcc和微软编译器(如果不可能写)

The solution must work on gcc and microsoft compiler (if it isn't possible to write it in standard C++).

推荐答案

好,现在你的问题更清楚了: - )

Ok, now your problem is clearer :-)

看一下boost :: iterator_facade和boost :: iterator适配器。它们实现了完整的迭代器接口和你的 cycle_iterator 只是为了实现一些方法,如increment(),decrement():

Take a look at boost::iterator_facade and boost::iterator adaptor. They implement the full iterator interface and your cycle_iterator only as to implement a few methods like increment(), decrement():

template<class IteratorBase>
class cycle_iterator 
     : public boost::iterator_adaptor< 
          cycle_iterator,     // the derived class overriding iterator behavior
          IteratorBase,       // the base class providing default behavior
          boost::use_default, // iterator value type, will be IteratorBase::value_type
          std::forward_iterator_tag, // iterator category
          boost::use_default  // iterator reference type
       > 
{
  private:
     IteratorBase m_itBegin;
     IteratorBase m_itEnd;

  public:
     cycle_iterator( IteratorBase itBegin, IteratorBase itEnd ) 
       : iterator_adaptor_(itBegin), m_itBegin(itBegin), m_itEnd(itEnd)
     {}

     void increment() {            
        if(base_reference()==m_itEnd) {
         base_reference()=m_itBegin;
        } else {
          ++base_reference(); // increments the iterator we actually point at
        }
     }

     // implement decrement() and advance() if necessary
  };

这可能不编译,但应该让你开始。

This probably doesn't compile but should get you started.

编辑:

boost::iterator_adaptor 在少数函数方面实现了完整的迭代器接口。它提供了 increment() decrement() advance code>, distance_to() equal_to() dereference / code>使用传递给 iterator_adaptor 基类的基本迭代器。

boost::iterator_adaptor implements the full iterator interface in terms of few functions. It provides default implementations for increment(), decrement(), advance(), distance_to(), equal_to() and dereference() using the base iterator passed down to the iterator_adaptor base class.

如果你需要的是一个正向迭代器,只需要实现 increment()你到达结束迭代器。如果你以类似的方式实现 decrement(),循环迭代器可以是双向的。如果 IteratorBase 本身是一个随机访问迭代器,循环迭代器也可以是随机访问和方法 advance distance_to 必须使用模运算来实现。

If all you need is a forward iterator, only the increment() method must be implemented to wrap around once you reach the end iterator. A cyclical iterator can be bidirectional if you implement decrement() in a similar fashion. If IteratorBase is itself a random access iterator, the cycle iterator can also be random access and method advance and distance_to must be implemented using modulo operations.

这篇关于最简单的方法来做循环迭代器(循环器)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆