在C ++ 11中的端到端迭代器无效 [英] Past-the-end iterator invalidation in C++11

查看:119
本文介绍了在C ++ 11中的端到端迭代器无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++上最热门的文章迭代器无效规则声称,不清楚是否使用过去的迭代器即 end() cend() rend code>和 crend())根据与指向容器中的元素的常规迭代器相同的规则无效。这些针对2003年和2011年C ++的声明,都是针对讨论结束迭代器无效规则的帖子,其中接受的答案建议2003年标准在这个问题上含糊不清。这个结论是基于23.1 / 10中的一个注释(在 swap()的上下文中),这似乎意味着当规范没有明确提到过去的无效-end迭代器,它们可能会失效。

The most popular post on C++ Iterator invalidation rules claims that it's not clear if the past-the-end iterators (i.e., those returned by end(), cend(), rend(), and crend()) are invalidated according to the same rules as normal iterators, which point to elements in the container. These claims, made for both 2003 and 2011 C++, defer to a post discussing End iterator invalidation rules, where the accepted answer suggests that the 2003 standard is ambiguous on the matter. This conclusion is based on a comment in 23.1/10 (in the context of swap()) that seems to imply that when the spec does not explicitly mention invalidation of past-the-end iterators, they may be invalidated.

对该帖子的问题(通过mike-seymour)的评论表明,C ++ 11在这个问题上是明确的,在 deque s。我的问题是关于所有容器:

A comment on that post's question (by mike-seymour) suggests that C++11 is unambiguous on this matter, in the case of deques. My question is about all containers:


  • 在C ++ 11中,是否有任何容器操作可能使过去的迭代器,并且这种行为在语言规范中是不明确的吗?

换句话说,


  • 在执行容器操作之后,信任过去端迭代器的有效性,该容器操作不会说明它可能使过去端迭代器无效。

推荐答案


我的问题是关于所有容器:

My question is about all containers:


    <在C ++ 11中,有没有任何容器操作可能会使过去的迭代器无效,并且这种行为在
    语言规范中是不明确的?

我不知道你的意思是这种行为在语言规范中是不明确的 ,但是肯定有操作使过去的操作符无效(比如插入到 std :: vector std :: string )。

I am not sure what you mean with "where this behavior is ambiguous in the language specification", but there certainly are operations that invalidate past-the-end operators (like insert into a std::vector or std::string).


换句话说,

Said differently,


  • 在执行不说它的容器操作之后,过去端迭代器的有效性可能会使过去端迭代器失效

您可以像任何其他迭代器一样信任过去的迭代器:任何不会(无效)使迭代器无效的操作不会使它们失效。除了标准体育的可能性一个bug,那就是所有操作,它不说他们(可能)使操作符无效。

You can trust the past-the-end iterator like any other iterator: Any operation that does not (potentially) invalidate iterators won't invalidate them. Except for the possibility of the standard sporting a bug, that is all operations where it doesn't say that they (potentially) invalidate operators.

这篇关于在C ++ 11中的端到端迭代器无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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