通常,交换从不使迭代器无效.然而,当分配器不同时,另一个规则发挥作用.在这种情况下,行为取决于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()的替代规则.