C++迭代器到 const_iterator
在 C++ 中,如何从(某个容器类的)迭代器(该容器类的)获取 const_iterator?来自 insert_iterator
的 const_iterator
怎么样?生成的迭代器应该指向与原始迭代器相同的位置。
How do I acquire a const_iterator
(of some container class) from an iterator
(of that container class) in C++? What about a const_iterator
from an insert_iterator
? The resulting iterator
should point at the same spot as the original does.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
容器需要提供
iterator
作为可转换为const_iterator
的类型,因此您可以隐式转换:std::insert_iterator
是输出迭代器。这无法将它们转换为常规的 Container::iterator ,它必须是前向迭代器。另一种插入迭代器可能允许这样的事情,但从标准函数获得的那些则不允许。
我想您可以围绕
std::insert_iterator
编写自己的包装器,以公开受保护的成员iter
,不过:Containers are required to provide
iterator
as a type convertible toconst_iterator
, so you can convert implicitly:std::insert_iterator
s are output iterators. This gives no way to convert them to a regularContainer::iterator
which must be a forward iterator.Another kind of insert iterator may allow such a thing, but those obtained from the standard functions don't.
I guess you can write your own wrapper around
std::insert_iterator
that exposes the protected memberiter
, though:您可以转换它们。示例:
但我想这不是您正在寻找的答案。显示代码。 :-)
You can convert them. Example:
But I guess that is not the answer you are seeking. Show me code. :-)