Introduction General Error Traversal Qmap method to summarize reference of improved traversal Qmap method
Introduction
Traverse Qmap in the normal way, and delete an access violation when "read location 0xXXX" occurs when the condition element is met. View the call stack pointing to qmap<int,int>::iterator::operator++ () and Qmapnode<int,int>::nextnode ()
Locate to delete the iterator traversal exception caused by the element in iterator, as follows: General error traversal Qmap method
Qmap<int,int>::iterator ITER; Traverse map for
(iter = Timermap_t1_i->begin (); Iter!= timermap_t1_i->end (); iter++)
{
if (tempnr>= Iter.key ())//If a key value pair sends an ordinal number less than or equal to the TEMPNR corresponding to the T1 timer reset and moves out the key value pair
{
KillTimer (Iter.value ());
Timermap_t1_i->erase (ITER);
}
The error is that the ITER pointer expires after the element has been deleted, and returns to the Timermap_t1_i->end () error-Qmap method when comparing to the for statement ()
Qmap<int,int>::iterator ITER; Traverse map for
(iter = Timermap_t1_i->begin (); Iter!= timermap_t1_i->end ();)
{
if (Tempnr>=iter.key ())//If a key value pair sends an ordinal number less than or equal to the TEMPNR corresponding to the T1 timer reset and moves out the key value pair
{
KillTimer (Iter.value ());
Timermap_t1_i->erase (iter++); Satisfies the delete condition, deletes the current node, and points to one of the following nodes
}
else
{
The iter++;//condition is not satisfied, point to the following node}
}
In the map in Timermap_t1_i->erase (iter++), when the ITER is deleted, the ITER is cached as Iter1, then the iter++ is directed to the next node, and then the erase function body is removed. The ITER used for deletion is actually a cached iter1 (that is, the last node of the current ITER (ITER) point to which it was added).
This and timermap_t1_i->erase (ITER); iter++; This sequence of execution is not the same. The former was added to the erase before execution, it is safe to add operations before ITER is removed (expired), the latter is performed after erase execution, and ITER has been removed (the current iterator has failed), adding to an already defunct iterator, Behavior is unpredictable, which is bound to cause the failure of the map operation and cause the process to be abnormal. Summary Qmap after erase, the current iterator will fail the attribute reference of the + + operator before and after
An efficient traversal deletion method for STL map
Using Iterator/reverse_iterator in traversal for Erase usage