I recently looked at STL container, and I am always confused about the failure of iterator. I have done some experiments and gained some understanding. I will summarize them as follows for the sake of memo.
Iterator is an object that can perform pointer-like operations on it (for example, removing the reference (operator * () and increasing (operator ++, we can understand it as a pointer. But it is not a common pointer. We can call it a generalized pointer. You can view it through sizeof (vector: iterator), and the memory occupied is not 4 bytes.
First, for vector, adding and deleting operations may invalidate some or all of the iterators of the container. Why does the iterator fail? Vector elements are stored sequentially in the memory. Imagine: if there are already 10 elements in the current container, we need to add another element to the container, however, there is no free space behind the 10 elements in the memory, and the elements of the vector must be stored sequentially while accessing the index. Therefore, we cannot find a place in the memory to store this element. Therefore, the vector must re-allocate the bucket to store the original elements and newly added elements: the elements stored in the old bucket are copied to the new bucket, and new elements are inserted, finally, cancel the old bucket. In this case, all iterators of the vector container will become invalid.
We can see that the above-mentioned method of allocating and revoking memory space achieves the self-growth of vector, and the efficiency is extremely low. To enable the vector container to implement fast memory allocation, the actually allocated container will have more space than the space currently required. The vector container reserves these additional storage areas to store newly added elements, instead of allocating a new bucket each time. You can see this mechanism from the implementation of capacity and reserve in vector.
Difference between capacity and size: size indicates the number of elements currently owned by the container, while capacity indicates the total number of elements that the container can store before it has to allocate a new bucket.
Several Failures of the vector iterator:
1. When an element is inserted (push_back), The iterator returned by the end operation is definitely invalid.
2. When an element is inserted (push_back), the return value of capacity is different from that before the element is inserted. Then, the entire container needs to be reloaded, And the iterator returned by the first and end operations will become invalid.
3. After the delete operation (erase, pop_back) is performed, all the iterators pointing to the delete vertex are invalid. All the iterators pointing to the elements behind the delete vertex are also invalid.
Deque iterator failure:
In C ++ Primer:
1. inserting elements at the front or end of the deque container does not invalidate any iterator.
2. Deleting an element at its header or tail will only invalidate the iterator pointing to the deleted element.
3. The insert and delete operations at any other location of the deque container will invalidate all iterators pointing to the container element.
However, I found in vs2005 that the first one was not satisfied. Why? Wait for the STL to go deep and then understand it slowly!
Only list iterators seem to be ineffective in rare cases. Maybe when you delete the node, the iterator pointing to the deleted node will become invalid. Nothing else has been found.
Let's first look at two regulations:
1. for deletion of the node-type container (MAP, list, set) element, the insert operation will invalidate the iterator pointing to this element, and the iterator of other elements will not be affected.
2. Deletion and insertion of the vector element will invalidate the iterator pointing to this element and subsequent elements.
Common error code example:
1 struct smem
2 {
3 int m_ I;
4 smem (INT I = 10)
5 {
6 m_ I = I;
7}
8
9 int Geti ()
10 {
11 return m_ I;
12}
13 };
1
2 typedef vector <smem *> smemlist;
3 typedef vector <smem *>: iterator sit;
4 smemlist memlist;
1 // The iterator is invalid.
2 For (sit it = memlist. Begin (); it! = Memlist. End (); ++ it)
3 {
4 If (* It)-> Geti () = 10)
5 memlist. Erase (it );
6 else
7 {
8 printf ("% d: % d/N", I ++, (* It)-> Geti ());
9}
10}
Two solutions:
For ordered containers:
Bool limit 10 (smem * pmem)
{
Return pmem-> Geti () = 10? True: false;
}
1memlist. Erase (remove_if (memlist. Begin (), memlist. End (), limit 10 ));
For node containers:
1 For (sit it = memlist. Begin ();
2 It! = Memlist. End ();)
3 {
4 If (* It). Second-> Geti () = 10)
5 memlist. Erase (it ++ );
6 else
7 {
8 printf ("% d: % d/N", I ++, (* It). Second-> Geti ());
9 It ++;
10}
11}