我正在实现一个通用列表:List with two types of iterators: List<T>::Iterator
和 List<T>ConstIterator
.
我有以下方法:
typename List<T>::Iterator List<T>::begin() {}
typename List<T>::ConstIterator List<T>::begin() const {}
这是为非常量列表返回非常量迭代器和为常量列表返回常量迭代器的合适方法吗?
最佳答案
是的,这也是完成的方式in the standard library .
类似于 C++ 11 中引入的标准库扩展,您可以额外提供一个 cbegin()
函数,allows获取 const 迭代器,即使列表本身不是 const。
关于c++ - 为链表返回正确类型的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18876644/