end() 在 STL 容器中是如何实现的? [英] How is end() implemented in STL containers?

查看:33
本文介绍了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++)

假设 i 是容器 v 的迭代器.

assuming i is an iterator for container v.

我的问题是什么保证结束总是指向容器中最后一个元素之后的一个?"STL 如何确保这种行为,这种情况是否有可能不是真的?

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?

推荐答案

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

STL ensures this behavior by always storing stuff like this:

最后(双关语),end() 是什么并不重要,只要总是end()(显然,不能与任何其他节点混淆).

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

这篇关于end() 在 STL 容器中是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆