map / set iterator not incrementablemap / set iterator not incrementable [英] map/set iterator not incrementablemap/set iterator not incrementable

查看:427
本文介绍了map / set iterator not incrementablemap / set iterator not incrementable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Driver ::〜Driver()
{
AutoCritSec acsDriverList(m_csDriverList,true);
DRIVERLIST :: iterator it = m_DriverList.begin();
for(; it!= m_DriverList.end(); it ++)
{
if(it-> second == this)
{
m_DriverList.erase (它);
it = m_DriverList.begin();
}
}
}

visual studio 2003,我的程序运行良好和良好。但是当我在2010年做同样的事情,然后在关闭应用程序时,我得到一些错误像

 表达式:map / set iterator not incrementable 

当我按忽略这个我获得

 表达式:standard c ++ library out of range&& 0 

有没有人知道这里发生了什么:我将非常感谢任何建议任何人。感谢和温暖的愿望。

解决方案

如果



从列表中删除后,您重置 it = m_DriverList.begin(); 。这可以。然后对循环表达式进行求值( for 语句)中的 i ++



超过容器末尾的迭代器会导致程序显示未定义的行为。最近版本的Visual C ++有助于在程序的调试版本中检测许多常见的迭代器错误,并提出断言以帮助您解决它们。



您可以通过删除循环来解决问题表达式并将其移动到 else 语句:

  while = m_DriverList.end())
{
if(it-> second == this)
{
m_DriverList.erase
it = m_DriverList.begin();
}
else
{
++ it;然而,每次你删除一个元素时重新开始迭代是一个重要的元素。相当浪费。考虑改用使用 erase 调用返回的迭代器:

  it = m_DriverList.erase(it); 


Driver::~Driver()
{
    AutoCritSec acsDriverList(m_csDriverList,true);
    DRIVERLIST::iterator it = m_DriverList.begin();
    for(;it!=m_DriverList.end();it++) 
    {
        if (it->second == this) 
        {
            m_DriverList.erase(it);
            it = m_DriverList.begin();
        }
    }
}

When I compile my program in visual studio 2003, my program behaves well and good. but when i do the same in 2010, then while closing the application i get some error like

Expression:map/set iterator not incrementable

and when i press to ignore this i get

Expression:"standard c++ library out of range" && 0

Does any one has any idea what is going on here: I will extremely indebted for any suggestions by anyone. Tons of thanks and warm wishes.

解决方案

If this is the only element in the list, you will overrun the end of the list.

After you remove this from the list, you reset it = m_DriverList.begin();. This is fine. Then the loop expression is evaluated (the i++ from the for statement), which causes it to be advanced past the end of the range.

Advancing an iterator past the end of the container causes the program to exhibit undefined behavior. Recent versions of Visual C++ helpfully detect many common iterator errors in debug builds of your program and raise assertions to help you to solve them.

You can resolve the problem by removing loop expression and moving it into an else statement:

while (it != m_DriverList.end())
{
    if (it->second == this)
    {
        m_DriverList.erase(it);
        it = m_DriverList.begin();
    }
    else
    {
        ++it;
    }
}

Though, restarting iteration every time you remove an element is rather wasteful. Consider instead using using the iterator returned by the call to erase:

it = m_DriverList.erase(it);

这篇关于map / set iterator not incrementablemap / set iterator not incrementable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆