Iterator invalidation(迭代器失效)

一.vector

所有读操作、swap、std::swap:都不会引起迭代器失效...

clear、operator=、assign:都会引起全部变量迭代器失效

reserve、shrink_to_fit:如果capacity的大小被改变了,则引起全部变量迭代器失效

erase:被删除的变量以及其后面的变量包括end()都迭代器失效

push_back、emplace_back:假如capacity的大小被改变,则引起全部变量迭代器失效。否则只是end()迭代器失效

insert、emplace、resize:假如capacity的大小被改变,则引起全部变量迭代器失效。否则只是在插入位置后面的变量迭代器失效

pop_back:被删除的变量以及end()迭代器失效

二.deque(没有发生删除且只插入在末尾,指针和引用不失效)

All read only operationsNever
swapstd::swapThe past-the-end iterator may be invalidated (implementation defined)
shrink_to_fitclearinsertemplacepush_frontpush_backemplace_frontemplace_backAlways
eraseIf erasing at begin - only erased elements

If erasing at end - only erased elements and the past-the-end iterator
Otherwise - all iterators are invalidated (including the past-the-end iterator).

resizeIf the new size is smaller than the old one : only erased elements and the past-the-end iterator

If the new size is bigger than the old one : all iterators are invalidated
Otherwise - none iterators are invalidated.

pop_frontOnly to the element erased
pop_backOnly to the element erased and the past-the-end iterator
Invalidation notes
    • When inserting at either end of the deque, references are not invalidated by insert and emplace.
    • push_frontpush_backemplace_front and emplace_back do not invalidate any references to elements of the deque.
    • When erasing at either end of the deque, references to non-erased elements are not invalidated by erasepop_front and pop_back.
    • A call to resize with a smaller size does not invalidate any references to non-erased elements.
    • A call to resize with a bigger size does not invalidate any references to elements of the deque.

转载于:https://www.cnblogs.com/Call-C/p/6979148.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值