和std ::向量v1(a1)和v2(a2)
那么v1.swap(v2)将使所有迭代器无效.
这是预期的行为吗?
解决方法
来自[container.requirements.general](来自n3290的文字):
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
,orallocator_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 orallocator_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()的替代规则.