end()在STL容器中是如何实现的?

发布于 2024-09-24 20:53:02 字数 227 浏览 0 评论 0原文

因此,当我们需要从头到尾遍历容器时,我们会编写类似

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月棠 2024-10-01 20:53:03

您询问的是所有 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.

七度光 2024-10-01 20:53:03

正如之前的一些海报所说,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 use iter = container.end() - 1; Otherwise, in the case of vectors, variable = someVector.back(); Assuming that variable is of the data type someVector 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.

絕版丫頭 2024-10-01 20:53:02

STL 通过始终存储如下内容来确保此行为:

vector

最后(双关语),什么并不重要 < code>end() ,只要它始终是 end() (并且,显然,不能混淆与任何其他节点)。

STL ensures this behavior by always storing stuff like this:

vector

In the end (pun), it doesn't matter what end() is, as long as it's always end() (and, obviously, can't be confused with any other node).

九命猫 2024-10-01 20:53:02

C++03 部分 23.1/7

begin() 返回一个引用容器中第一个元素的迭代器。

end() 返回一个迭代器,它是容器的过去的值。

如果容器为空,则begin() == end();

C++03 Section 23.1/7 says

begin() returns an iterator referring to the first element in the container.

end() returns an iterator which is the past-the-end value for the container.

If the container is empty, then begin() == end();

め可乐爱微笑 2024-10-01 20:53:02

stl 规范保证 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.

沧桑㈠ 2024-10-01 20:53:02

“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文