C++迭代器、默认初始化以及用作未初始化哨兵的内容
上下文
我有一个由地图和矢量组合在一起的自定义模板容器类。映射将字符串解析为序数,向量将序数解析为条目(仅完成初始字符串到序数的查找,将来的引用将指向向量)。这些条目被侵入性地修改为包含“分配”的“bool”和一个 iterator_type(容器类映射的 const_iterator)。
我的容器类将使用 RCF 的序列化代码(模拟 boost::serialization)来序列化我的容器类到网络中的节点。序列化迭代器是不可能的,或者是一堆蠕虫,一旦向量和映射在远程站点上序列化,我就可以轻松地重新生成它们。
问题
我需要默认初始化,并且能够测试迭代器是否尚未被分配(如果分配了,则有效,如果没有分配,则无效)。由于地图迭代器不会在对其执行操作时失效(当然,除非项目被删除:D),所以我是否假设 map
是一个有效的哨兵(无论地图的状态(即,它可能为空)初始化为?
我将始终可以访问父地图,我只是不确定 end()
是否与地图内容更改相同。
我不想使用另一级间接(即 boost::可选)来实现我的目标,我宁愿放弃编译器检查以纠正逻辑,但如果我不需要的话那就太好了。
其他
这个问题存在,但大多数其内容似乎毫无意义。根据 g++ 和 clang++,将 NULL 分配给迭代器是无效的。
这是另一个类似的问题,但它重点关注迭代器的常见用例,通常倾向于使用迭代器进行迭代,当然在这个用例中,容器的状态在迭代进行时并不意味着改变。
The Context
I have a custom template container class put together from a map and vector. The map resolves a string to an ordinal, and the vector resolves an ordinal (only an initial string to ordinal lookup is done, future references are to the vector) to the entry. The entries are modified intrusively to contain a a bool
"assigned" and an iterator_type which is a const_iterator to the container class's map.
My container class will use RCF's serialization code (which models boost::serialization) to serialize my container classes to nodes in a network. Serializing iterator's is not possible, or a can of worms, and I can easily regenerate them onces the vectors and maps are serialized on the remote site.
The Question
I need to default initialize, and be able to test that the iterator has not been assigned to (if it is assigned it is valid, if not it is invalid). Since map iterators are not invalidated upon operations performed on it (unless of course items are removed :D) am I to assume that map<x,y>::end()
is a valid sentinel (regardless of the state of the map -- i.e., it could be empty) to initialize to ?
I will always have access to the parent map, I'm just unsure wheather end()
is the same as the map contents change.
I don't want to use another level of indirection (--i.e., boost::optional) to achieve my goal, I'd rather forego compiler checks to correct logic, but it would be nice if I didn't need to.
Misc
This question exists, but most of its content seems non-sense. Assigning a NULL to an iterator is invalid according to g++ and clang++.
This is another similar question, but it focuses on the common use-cases of iterators, which generally tends to be using the iterator to iterate, ofcourse in this use-case the state of the container isn't meant to change whilst iteration is going on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的想法基本上是正确的。仅当地图迭代器指向的元素被删除时,地图迭代器才会失效。
map.end()
不指向元素,因此永远不会失效。但它不是地图迭代器的默认值。每个地图对象都有自己的
end()
,并且它们不具有可比性。这对您来说不应该是问题;只是要小心。You basically got the idea right. A map iterator is only invalidated if the element it points to is removed.
map.end()
doesn't point to an element, and therefore is never invalidated.It is NOT the default value of a map iterator, though. Each map object has its own
end()
, and they are not comparable. This shouldn't be a problem for you; just be careful.