c++ iterator 失效_c++迭代器失效的问题汇总(Summary of c++ iterator failures).doc

c迭代器失效的问题汇总(Summaryofciteratorfailures)

c++迭代器失效的问题汇总(Summary of c++ iterator failures)

Summary of C++ iterator failures

First, for vector, adding and deleting operations can cause partial or full iterator invalidation of the container. So why does the iterator fail? The vector element is stored in memory, if the current container has 10 elements, now have to add an element to a container, but the memory behind these 10 elements have a free space, and vector elements must be stored sequentially while access by index, so we can't in memory just to find the element local storage. So vector must be re allocation of storage space, used to store the original elements and the newly added: stored in the storage space of the old elements are copied to the new storage space, then inserts a new element, finally cancelled the old storage space. This happens and must cause all iterators in the vector container to fail. We see how the implementation of the above mentioned allocation and cancellation of memory space achieves the self growth of vector, and the efficiency is extremely low. In order to make the vector container to achieve fast allocation, the actual distribution of the container will be more than the needed space, the vector container reserved this extra storage area for storing newly added elements, without the need for each re allocation of new storage space. You can do this from vector, and capacity and reserve members can see the mechanism. The difference between capacity and size: size is the number of elements that the container currently has, and capacity refers to the total number of elements that the container can store before it must allocate new storage space.

The failure of the vector iterator:

1. when an element is inserted (push_back), the iterator returned by the end operation is certainly invalid.

2. when an element is inserted (push_back), the capacity return value has changed before it has been inserted, and the entire container needs to be reloaded, when the iterator returned by the beg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值