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

前端之家收集整理的这篇文章主要介绍了c – 应该是std :: vector :: swap()与有状态的分配器无效所有的迭代器?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
给定分配器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()的替代规则.

猜你在找的C&C++相关文章