Delete data in the STL container

Original link: http://www.cnblogs.com/mingzhang/p/10857646.html

STL divided into two categories according to the storage container, one is stored in the form of an array by a container (eg: vector, deque); the other is stored in the form of discrete containers node (eg: list, set, map). When using the erase method to remove the element to note some problems.

 

1.list, set, map container

     Can be used as such in the use list, set or remove certain elements traverse the map:

 

1.1 correct wording 1

Copy the code
 1 std::list< int> List;
 2 std::list< int>::iterator itList;
 3 for( itList = List.begin(); itList != List.end(); )
 4 {
 5       if( WillDelete( *itList) )
 6       {
 7             itList = List.erase( itList);
 8        }
 9        else
10             itList++;
11 }
Copy the code
 

1.2 correct wording 2

Copy the code
 1 std::list< int> List;
 2 std::list< int>::iterator itList;
 3 for( itList = List.begin(); itList != List.end(); )
 4 {
 5       if( WillDelete( *itList) )
 6       {
 7           List.erase( itList++);
 8       }
 9       else
10           itList++;
11 }
Copy the code

1.3 Error writing 1

Copy the code
1 std::list< int> List;
2 std::list< int>::iterator itList;
3 for( itList = List.begin(); itList != List.end(); itList++)
4 {
5      if( WillDelete( *itList) )
6      {
7           List.erase( itList);
8      }
9 }
Copy the code
 

1.4 Error writing 2

Copy the code
 1 std::list< int> List;
 2 std::list< int>::iterator itList;
 3 for( itList = List.begin(); itList != List.end(); )
 4 {
 5      if( WillDelete( *itList) )
 6      {
 7           itList = List.erase( ++itList);
 8       }
 9       else
10           itList++;
11 }
Copy the code
 

1.5 Analysis

Proper use Method 1: erase the return value to obtain the position of the next element of
the proper use of 2: first use "+" before calling the erase method to obtain the position of the next element of
the wrong use of 1: call the erase method after using "++" to get the location of the next element, due after the call to erase method, the position of the element has been deleted, if in a position to get the next according to the old location, an exception occurs.
Misuse Method 2: Same as above.

2. vector, deque container

When using vector, deque traverse remove elements, can also erase the return value to obtain the position of the next element:

2.1 correct wording

Copy the code
1 std :: vector <int> more; 
 2 std :: vector <int> :: iterator itVec; 
 3 for (itVec = Vec.begin (); itVec! = Vec.end ()) 
 4 { 
 5 if (WillDelete (* itVec)) 
 6 { 
 7 itVec = Vec.erase (itVec); 
 8} 
 9 else 
10 itList ++; 
11}
Copy the code

2.2 Note

Note meaning: vector, deque not like the way above the "proper use of 2" to traverse deleted. Please refer to the reason Effective STL clause 9. To the following excerpt:

1) For the associative containers (such as a map, set, multimap, multiset), delete the current iterator, only the current iterator will fail, as long as the erase, the current iterator can be incremented. This is because the map like container, using a red-black tree is implemented, insert, delete a node will not affect other nodes.

 

Copy the code
1 for (iter = cont.begin(); it != cont.end();)
2 {
3    (*iter)->doSomething();
4    if (shouldDelete(*iter))
5       cont.erase(iter++);
6    else
7       ++iter;
8 }
Copy the code

 

Because iter passed erase method is a copy, iter ++ will point to the next element.
2) For the sequence containers (e.g., vector, deque), remove all of the elements of the current iterator iterator will later are unavailable. This is because vetor, deque uses contiguous memory allocation, leading to delete an element behind all the elements will move forward one position. Some erase method can return the next valid iterator.

Copy the code
1 for (iter = cont.begin(); iter != cont.end();)
2 {
3    (*it)->doSomething();
4    if (shouldDelete(*iter))
5       iter = cont.erase(iter); 
6    else
7       ++iter;
8 }
Copy the code

3) For the list, it is not continuous use of the memory allocation, and its erase method also returns the next valid Iterator, so the above two methods may be used.

3. iterator failure

3.1 vector

Internal data structures: an array.
Each element of random access, the required time constant.
In addition or deletion of the time regardless of the number of elements required for the end elements, the time required to add or remove the number of elements with a linear variation at the beginning or in the middle of the element.
Dynamically increase or decrease the element, the memory management automatically, but the programmer can use the reserve () member functions to manage memory.
vector iterator when the memory is reallocated to fail (element no longer the same as it points before and after the operation). When over capacity () - when the size () elements inserted into the vector, the memory will be reallocated, all of the iterator will fail; otherwise, the iterator points to any element of the current element in the future will fail. When you delete an element, point to any element element to be removed after the iterator will fail.

3.2 and

Internal data structures: an array.
Each element of random access, the required time constant.
Adding elements to the beginning and end of the time required regardless of the number of elements, the time required to add or remove the number of elements varies linearly with the intermediate element.
Dynamically increase or decrease the element, the memory management automatically, member functions not provided for memory management.
Any element will increase deque iterators failure. Removing elements iterator will fail in the middle of the deque. When the deque head or tail removing elements, only the element iterator fails.

3.3 list

Internal data structures: an annular bidirectional linked list.
You can not access a random element.
Two-way traversal.
Time to add or remove elements at the beginning, middle and end of anywhere required are constant.
Elements can be dynamically increased or decreased, memory management automatically.
Will not increase any element iterator failure. When you delete an element, in addition to pointing to the current element to be removed iterators, the other iterators will not fail.

3.4 slist

Internal data structures: a one-way linked list.
Not two-way traversal, only traverse from front to back.
Other features similar to the same list.

3.5 stack

Adapter, which can convert any type of container of a stack sequence is generally used as a support for the container deque sequence.
Elements can only last in first out (LIFO).
You can not traverse the entire stack.

3.6 queue

Adapter, which can be any type of container is converted to a queue sequence is generally used as a support for the container deque sequence.
Elements can only FIFO (FIFO).
You can not traverse the entire queue.

3.7 priority_queue

Adapter, which can convert any type of container is a sequence of priority queue, is generally used as the underlying storage vector.
Can only access the first element, you can not traverse the entire priority_queue.
The first element is always an element of the highest priority.

3.8 set

Keys and values are equal.
Unique.
Default elements in ascending order.
If the iterator is pointing element is deleted, the iterator fails. Any increase in other operating delete elements do not cause the failure of iterators.

3.9 multiset

Key is not unique.
Other features of the same set.

3.10 hash_set

Compared with the set, it contains the elements is not necessarily sorted, but in accordance with the hash function used in dispatch, it can provide faster search speed (of course with the hash function related).
Other features of the same set.

3.11 hash_multiset

Key is not unique.
Other features and hash_set same.

3.12 map

Unique.
The default key elements in ascending order.
If the iterator is pointing element is deleted, the iterator fails. Any increase in other operating delete elements do not cause the failure of iterators.

3.13 multimap

Key is not unique.
Other characteristics and the same map.

3.14 hash_map

Compared with the map, it is not necessarily the key elements inside the sort of value, but in accordance with the hash function used in the assignment, it can provide faster search speed (of course also with the hash function).
Other characteristics and the same map.

3.15 hash_multimap

Key is not unique.
Other features and hash_map same.

Reproduced in: https: //www.cnblogs.com/mingzhang/p/10857646.html

Guess you like

Origin blog.csdn.net/weixin_30642561/article/details/94998364