应该 C++迭代器能够在传递最后一项后递减吗?
我一直在修改我的容器以兼容 STL。我已经修改了迭代器以具有必要的功能。它们都是随机访问迭代器。目前它们可以与所有适用的 STL 算法配合良好。但是我的一个迭代器一旦变得无效(越界)将无法正常工作。有必要拥有这个属性吗?具体来说,我担心 end() 迭代器不能递减。请注意,它可以与其他有效迭代器进行比较,甚至可以计算距离。目前迭代器的大小是 4 个字节,如果不是真的有必要,我真的不想再添加 4 个字节。
预先感谢,
水泥
I have been modifying my containers to be STL compatible. I have modified my iterators to have the necessary functions. They are all random access iterators. Currently they work fine with all applicable STL algorithms. But one of my iterators will not be functional once it became invalid (out of bounds). Is it necessary to have this property? Specifically I am afraid of end() iterator which cannot be decremented. Notice that it can be compared and even distance can be calculated with other valid iterators. Currently the size of the iterator is 4 bytes, I really dont want to add another 4 if it is not really necessary.
Thanks in advance,
Cem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于
random_access_iterator
这是必需的。你必须实施它。具体来说,根据第 24.1.4.1 节,对于双向迭代器(其中随机访问迭代器是一个特化),递减必须始终有效。For a
random_access_iterator
this is required. You will have to implement it. Specifically, according to 24.1.4.1 for abidirectional_iterator
(of which arandom_access_iterator
is a specialization) decrementing always has to be valid.std::reverse_iterator 依赖于container.end() 才有效且可递减。
std::reverse_iterator depends on container.end() to be valid and being decrement-able.