Traversal Qmap throws Exception handling _QT

Source: Internet
Author: User
Tags exception handling

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.