【发布时间】:2012-02-10 14:24:04
【问题描述】:
我正在尝试实现循环迭代器,这在我的问题中非常有用。根据this 线程,我为此目的使用boost::iterator_adaptor,并且迭代器本身工作正常。实现很像this answer。
但是,在同一句中同时操作cycle_iterator 和“native”IteratorBase 时会出现一些问题,如下所示:
vector<int> v;
vector<int>::iterator it = v.begin();
cyclic_iterator<vector<int>::iterator> cit(v.begin(), v.end());
if (cit != it) // Don't compile
{
...
}
编译器生成错误 C2678: binary '!=' : no operator found 接受'cyclic_iterator' 类型的左操作数(或没有可接受的转换)。
我可以在cyclic_iterator 中为IteratorBase 明确指定operator!= 来解决它。但是,我需要显式重载operator==、operator= 等等。
有没有更方便的方法可以让这些东西发挥作用?
【问题讨论】: