链表常量迭代器
Linked list const iterator
我实现了一个双向链表,并创建了一个扩展std::iterator
的迭代器。我现在正在尝试创建一个const
版本。
我试过了:
typename typedef list_iterator<T_> iterator;
typename typedef list_iterator<T_> const const_iterator;
但是,如果我这样做,则会出现此错误:
error C2678: binary '--' : no operator found which takes a left-hand operand of type 'const list_iterator<T_>' (or there is no acceptable conversion)
这是我operator--
:
list_iterator& operator -- ()
{
_current = _current->_previous;
return *this;
}
list_iterator operator--(int) // postfix
{
list_iterator hold = *this;
--*this;
return list_iterator( hold );
}
如果我把
list_iterator operator--() const
。我无法修改_current
的值
我如何让我的迭代器现在像const_iterator
一样工作,以便我可以从我的链表中调用 获取 begin()
和 end()
的 const 版本,以及 cbegin()
和 cend()
?
对。 问题是你对const_iterator类型def的声明。 (请参阅如何正确实现自定义迭代器和const_iterators?
而不是
typename typedef list_iterator<T_> const const_iterator;
你想要
typename typedef list_iterator<const T_> const_iterator;
相关文章:
- 是否应避免从非常量迭代器转换为常量迭代器?
- 如何实现常量迭代器?
- 使用迭代器替换映射中的常量项的方法
- boost_multi_index迭代器取消引用给出常量
- 是否可以重用通常的迭代器来构建常量迭代器?
- 使用 declval 获取非常量迭代器
- 为什么在此代码中隐式转换为常量迭代器失败?
- const_iterator<T>和迭代器<常量T>有什么区别?
- 常量迭代器的问题
- 链表常量迭代器
- 输入迭代器必须具有常量解引用运算符吗
- 属性树常量迭代器的索引
- 递增常量前向迭代器
- C++中非常量列表迭代器的奇怪行为
- 如何正确地声明从非常量迭代器到指针的常量指针
- 列表的迭代器可以返回非常量引用吗?
- 无法取消引用映射常量迭代器的mapped_type
- 强制 foreach 使用常量迭代器
- 为什么这个迭代器常量
- std::设置迭代器常量限定符