Does moving a vector invalidate iterators?

前端 未结 4 1883
孤独总比滥情好
孤独总比滥情好 2020-11-28 07:34

If I have an iterator into vector a, then I move-construct or move-assign vector b from a, does that iterator still point to the same

4条回答
  •  再見小時候
    2020-11-28 08:28

    I think the edit that changed move construction to move assignment changes the answer.

    At least if I'm reading table 96 correctly, the complexity for move construction is given as "note B", which is constant complexity for anything except std::array. The complexity for move assignment, however, is given as linear.

    As such, the move construction has essentially no choice but to copy the pointer from the source, in which case it's hard to see how the iterators could become invalid.

    For move assignment, however, the linear complexity means it could choose to move individual elements from the source to the destination, in which case the iterators will almost certainly become invalid.

    The possibility of move assignment of elements is reinforced by the description: "All existing elements of a are either move assigned to or destroyed". The "destroyed" part would correspond to destroying the existing contents, and "stealing" the pointer from the source -- but the "move assigned to" would indicate moving individual elements from source to destination instead.

提交回复
热议问题