加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 应该是std :: vector :: swap()与有状态的分配器无效所有的

发布时间:2020-12-16 06:44:52 所属栏目:百科 来源:网络整理
导读:给定分配器a1和a2,其中a1!= a2, 和std ::向量v1(a1)和v2(a2) 那么v1.swap(v2)将使所有迭代器无效. 这是预期的行为吗? 解决方法 通常,交换从不使迭代器无效.然而,当分配器不同时,另一个规则发挥作用.在这种情况下,行为取决于allocator_traits a1 :: propaga
给定分配器a1和a2,其中a1!= a2,

和std ::向量v1(a1)和v2(a2)

那么v1.swap(v2)将使所有迭代器无效.

这是预期的行为吗?

解决方法

通常,交换从不使迭代器无效.然而,当分配器不同时,另一个规则发挥作用.在这种情况下,行为取决于allocator_traits< a1> :: propagate_on_container_swap :: value和allocator_traits< a2> :: propagate_on_container_swap :: value.如果两者均为真,则分配器与数据一起进行交换,所有迭代器仍然有效.如果任何一个都是假的,行为是未定义的,那么允许VC 2010展示的特定行为.

来自[container.requirements.general](来自n??3290的文字):

Allocator replacement is performed by copy assignment,move assignment,or swapping of the allocator only if allocator_traits<allocatortype>::propagate_on_container_copy_assignment::value,
allocator_traits<allocatortype>::propagate_on_container_move_assignment::value,or allocator_traits<allocatortype>::propagate_on_container_swap::value is true within the implementation of the corresponding container operation. The behavior of a call to a container’s swap function is undefined unless the objects being swapped have allocators that compare equal or allocator_traits<allocatortype>::propagate_on_container_swap::value is true.

Every iterator referring to an element in one container before the swap shall refer to the same element in the other container after the swap

Unless otherwise specified … no swap() function invalidates any references,pointers,or iterators referring to the elements of the containers being swapped.

23.3.6.5没有指定vector :: swap()的替代规则.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读