splice()on std :: list和iterator无效 [英] splice() on std::list and iterator invalidation

查看:383
本文介绍了splice()on std :: list和iterator无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

list :: splice()的三参数形式将单个元素从一个列表移动到另一个列表。 SGI的文档明确声明所有迭代器,包括指向元素的正在移动仍然有效。 Roguewave的文档没有说明有关 splice()的迭代器无效属性的任何信息方法,而C ++标准明确地声明它使所有迭代器和对被拼接元素的引用无效。

The 3-argument form of list::splice() moves a single element from one list to the other. SGI's documentation explicitly states that all iterators, including the one pointing to the element being moved remain valid. Roguewave's documentation does not say anything about iterator invalidation properties of splice() methods, whereas the C++ standard explicitly states that it invalidates all iterators and references to the element being spliced.

splicing()在实践中的工作原理如SGI定义,但是我在微软的STL实现(严格遵循标准的字母)的调试/安全SCL版本中获得断言失败(解除引用无效迭代器)。

splicing() in practice works as defined by SGI, but I get assertion failure (dereferencing invalid iterator) in debug / secure SCL versions of microsoft's STL implementation (which strictly follows the letter of the standard).

使用列表,因为我想在列表之间移动元素,同时保留指向它的迭代器的有效性。该标准对原始SGI的规范做了极其无益的更改。

Now, I'm using list exactly because I want to move an element between lists, while preserving the validity of the iterator pointing to it. The standard has made an extremely unhelpful change to the original SGI's specification.

我如何解决这个问题?或者我应该是务实的,把我的头粘在沙子里(因为拼接不是在实践中无效迭代器 - 即使在MS的实现,一旦迭代器调试关闭)。

How can I work around this problem? Or should I just be pragmatic and stick my head in the sand (because the splicing does not invalidate iterators in practice -- not even in the MS's implementation, once iterator debugging is turned off).

推荐答案

好吧,这似乎是标准中的一个缺陷,根据链接。看起来贴在沙子里是一个很好的策略,因为它会在新的库版本中修复。

Ok, this seems to be a defect in the standard, according to this and this link. It seems that "sticking the head in the sand" is a good strategy, since it will be fixed in new library versions.

这篇关于splice()on std :: list和iterator无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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