c++ - 'iterator' 可以只输入子类 'const_iterator' 吗?

标签 c++ casting iterator containers

another question about iterators 之后我对自定义容器有一些疑问。在我的容器中,iteratorconst_iterator 的子类,因此我可以“免费”从非 const 转换为 const。但这是否允许,或者这样的设置是否有任何缺点或不工作的情况?

最佳答案

是的,这很好。这就是 VC10 对 vector 的迭代器的实现方式。例如,是结构化的。见 _Vector_iterator_Vector_const_iterator<vector> .

顺便说一句,编写迭代器很困难。值得您花时间学习和使用 boost::iterator 库。

关于c++ - 'iterator' 可以只输入子类 'const_iterator' 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2844466/

相关文章:

python - 读取文件并返回国家列表的方法

c++ - 使用列表迭代器排序

c++ - 忽略外部 vector 位置的嵌套 vector 迭代

c++ - 为什么迭代器需要 CopyConstructible 和 CopyAssignable?

c++ - Eigen 中立方根的性能改进

c++ - 如何使用 openCV 检测图像中的字母?

casting - 具体结构上的类型转换与类型断言?

c++ - 为什么 constexpr 隐式转换并不总是有效?

c++ - 更正 Visual C++ __FILE__ 宏的大小写

c++ - 如何在Qt无框窗口中实现QSizeGrip?