Summary of scenarios where STL iterators fail

1. The scenario where the serial container iterator fails

Sequential containers take vector as an example. The following cases will fail respectively:

1. push_back() invalidates the iterator.

​ Add an element at the end of the container. If the container has remaining space (capacity() > size()), add new elements directly to the end of the container. At this time, end() in the original iterator will be invalidated, and the others will not be invalidated . If the container has no remaining space (capacity() == size()), it will cause the container to reallocate memory, then copy the data from the original memory to the new memory, and then add new elements at the end. At this point, the original iterators (all) are invalidated due to memory reallocation .

2. pop_back() invalidates the iterator.

​ Delete the last element in the container directly, the iterator and end() of the last element in the original iterator will be invalidated, and the rest will not be invalidated .

3. insert(iterator, n) invalidates the iterator.

​ If there is space left in the container, first insert an element at the end of the container, then move the insertion point and subsequent elements backward by one bit, and then create a new element at the insertion point. Otherwise, it will cause the container to reallocate memory, then copy the element before the insertion point, create a new element at the insertion point, and then copy the element after the insertion point. Therefore, if there is no reallocation of memory, the insertion point in the original iterator and the iterators after the insertion point (including end()) are invalid. If there is a reallocation of memory, the original iterators (all) are invalidated .

4. erase(iterator) invalidates the iterator.

​ Move the deletion point and the following elements forward one bit, and then delete the last element. Therefore, the iterators before the deletion point in the original iterator are valid, and the element iterators after the deletion point are all invalid .

2. Associative container iterator failure scenario

set, multiset, map, multimap, use red-black tree to store data.

1. insert(iterator) insertion will not invalidate any iterator ;

2. The delete operation invalidates the iterator pointing to the deleted position, but does not invalidate other iterators . The erase iterator only invalidates the iterator of the deleted element, and the return value is the next iterator of the deleted element, so the iterator should be deleted by iter=contan.erase(iter) and erase(iter++). The deletion method of erase(iter++) is not available for containers with continuous memory, such as vector, because the next iterator of the deleted element is also invalid; iter=contan.erase(iter) is feasible.

3. Prevent exceptions caused by iterator failure

1. Delete multiple elements method:

for(vector<T>::iterator iter=veci.begin(); iter!=veci.end(); )
{
    
    
	if(判断删除的元素的条件)
	{
    
    
		{
    
    //元素类型T为原始指针时
			//T tmp = *iter;
			//delete tmp;
		}
		iter = veci.erase(iter);
	}
    else
	{
    
    
		iter ++ ;
	}
}

2. False deletion occurs when clear clears data
. The data in the container is gone, but the memory in the container is still there. That is, clear can clear the data and make the container size become 0, but it will not cause the container capacity to become 0.
clear will only clear the elements in the container. If the element is a pointer, the memory of the object in the heap managed by the pointer needs to be cleaned up manually.

//元素类型T为原始指针时
for(vector<T>::iterator iter=veci.begin(); iter!=veci.end(); )
{
    
    
	T tmp = *iter;
	delete tmp;
	iter = veci.erase(iter);
}

//元素类型T为对象或一版类型时,智能指针类型为对象,而非真正的指针
for(vector<T>::iterator iter=veci.begin(); iter!=veci.end(); )
{
    
    
	iter = veci.erase(iter);
}
//与clear等效
veci.clear();

If there are mistakes or deficiencies, welcome to comment and point out! Creation is not easy, please indicate the source for reprinting. If it is helpful, remember to like and follow (⊙o⊙)
For more content, please follow my personal blog: https://blog.csdn.net/qq_43148810

Guess you like

Origin blog.csdn.net/qq_43148810/article/details/127088856