c++ - 为链表返回正确类型的迭代器

标签 c++ iterator linked-list

我正在实现一个通用列表:List with two types of iterators: List<T>::IteratorList<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/

相关文章:

c++ - 如何实现 is_enum_class 类型特征?

c++ - 显式复制构造函数

c++ - 同一类型的多个声明会影响编译时间吗?

c++ - 是否有类似于循环(循环)迭代器的东西?

c - 双向链表中的插入和删除

c - Valgrind 通知我 C 中存在内存泄漏,我很确定我已修复该问题

c++ - Visual C++ 错误 C2143 : syntax error : missing ';' before 'constant'

c++ - 为什么 std::generate() 和 std::generate_n() 需要不同的迭代器?

php - 可迭代对象和数组类型提示?

java - 在java中,有没有办法搜索存储在链表中的类的特定字段?