end()在STL容器中是如何实现的?
因此,当我们需要从头到尾遍历容器时,我们会编写类似
for (i = v->begin(); i != v->end(); i++)
的
内容,假设 < code>i 是容器v
的迭代器。
我的问题是“什么保证 end 始终指向容器中最后一个元素之后的一个?” STL 如何确保这种行为?这种情况是否有可能不是真的?
So when we need to traverse a container from start to end we write something like
for (i = v->begin(); i != v->end(); i++)
assuming i
is an iterator for container v
.
My question is "what guarantees that end will always point to one past the last element in container?" How does STL ensures this behavior and is there any chance that this case is not true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您询问的是所有 STL 容器...没有提及向量,特别是 end() 可能 的实现,正如您显然直观地期望的那样。 std::map<> 的末尾是什么? “结束是最后使用的节点之后的一个”只是一个逻辑概念,表示您可以安全地从最后使用的节点开始递增,将其与“结束”的抽象概念区分/等同,并执行一些节点算术,其中 end 被认为比最后使用的节点更远。不要太从字面上理解。
You're asking about all STL containers... not a word of mention of vector specifically where end() might be implemented as you obviously intuitively expect. What's one past the end in a std::map<>? The "end is one past the last used node" thing is just a logical concept, expressing that you can safely increment from that last-used node, differentiate/equate it from/to the abstract concept of "end", and do some node arithmetic where end is considered to be one further along than the last-used node. Don't take it too literally.
正如之前的一些海报所说,
end()
是结束元素之后的一个。如果您需要通过迭代器访问最后一个元素,请使用 iter = container.end() - 1; 否则,对于向量,variable = someVector.back(); > 假设该变量属于someVector
包含的数据类型。至于如何保证它指向末尾,容器本身会在内部处理它。您只需像对待任何其他对象一样对待它,就像对待黑匣子一样,并相信它会正确执行。
每当调整容器大小时,它都会跟踪结束位置,并在您再次访问
end()
之前更新。然而,根据容器的不同,如果您有一个迭代器并以某些方式更改它,则可能会使迭代器无效并破坏您的迭代过程。As some of the previous posters have stated
end()
is one past the end element. If you need to access the last element via iterators useiter = container.end() - 1;
Otherwise, in the case of vectors,variable = someVector.back();
Assuming that variable is of the data typesomeVector
contains.In regard to what guarantees that it points to the end, the container itself handles that internally. You just have to treat it like a blackbox like any other object and trust it does it correctly.
Whenever the container is resized, it will track where the end is and will be up to date before you access
end()
again. Depending on the container however, if you have an iterator and alter it in some ways, it can invalidate the iterator and break your iteration process.STL 通过始终存储如下内容来确保此行为:
最后(双关语),什么并不重要 < code>end() 是,只要它始终是
end()
(并且,显然,不能混淆与任何其他节点)。STL ensures this behavior by always storing stuff like this:
In the end (pun), it doesn't matter what
end()
is, as long as it's alwaysend()
(and, obviously, can't be confused with any other node).C++03 部分
23.1/7
说C++03 Section
23.1/7
saysstl 规范保证 end 会超过 end 请参阅此处。情况永远如此。具体如何执行此操作取决于实现(例如,有时这些值只是设置为 null),但请放心,只要 v 是有效的指针,您的循环就可以正常进行。
The stl specification guarantees that end will be one past the end See here. That will always be the case. Exactly how it does this can depend on the implementation (sometimes the values is just set to null for example), but rest assured your loop will be OK as long as v is a valid pointer.
“end 将始终指向容器中最后一个元素之后的一个”意味着如果您递增指向最后一个元素的迭代器,它将等于
end()
的结果。实施可能有所不同。在 Visual C++ 中,std::vector::end()
返回持有零指针的实现特定迭代器。"end will always point to one past the last element in container" means that if you increment iterator that points to the last element it will be equal to the result of
end()
. Implementation can be different. In Visual C++std::vector::end()
returns implementation specific iterator that holds zero pointer.