STL:RunTime错误:map / set迭代器不兼容 [英] STL: RunTime Error : map/set iterators incompatible

查看:220
本文介绍了STL:RunTime错误:map / set迭代器不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





当我尝试迭代我的std:map时,我遇到了运行时错误。



这是课程。在dtor中,我需要清除地图

Hi,

I'm getting a run-time error when I try to iterate my std:map.

Here is the class. In the dtor, I need to clear the map

class CMyClass
{
public:
   CMyClass()
   {

   }
   void PopulateMap()
   {
    // populate map
   }
   ~CMyClass()
   {
    // clear map
    CSourceToIdMap::iterator itEvent = m_EventMap.begin();
   // Line below ERROR
    while(itEvent != m_EventMap.end()) // RunTime Error : map/set iterators incompatible
    {
      CIdToTargetMap * pIdToTargetMap = itEvent->second;
      m_EventMap.erase(itEvent++);
      delete pIdToTargetMap;
    }
   }  
private:
  typedef std::multimap<int, CPanel *> CIdToTargetMap;
  typedef std::map<CPanel *, CIdToTargetMap *> CSourceToIdMap;
  CSourceToIdMap m_EventMap;

};



问题:



这个问题似乎没有是一个迭代指向地图中的无效元素。



问题似乎是迭代器不兼容。



运行程序时,我在这个文件中得到一个断言:



c:\Program Files(x86)\ Microsoft Visual Studio 10.0 \ VC【包含\ text>



第321行:_DEBUG_ERROR(map / set iterators incompatible);




我已经尝试了这里提供的所有建议。我仍然得到ASSERT。



这是另一个讨论这个问题的链接:

http://stackoverflow.com/questions/3772304/runtime-error-map-set-iterators-incompatible [ ^ ]

但该链接中提到的解决方案对我来说并不清楚。< br $>




任何想法/建议?

提前致谢。


THE PROBLEM:

The problem does NOT seem to be that an iterating is pointing to an invalid element in the map.

The problem seems to be iterator being NOT compatible.

When run the program, I get an assert in this file:

c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree

Line 321: _DEBUG_ERROR("map/set iterators incompatible");


I have tried all the suggestions already provided here. I still get the ASSERT.

Here is another link that discuss this issue:
http://stackoverflow.com/questions/3772304/runtime-error-map-set-iterators-incompatible[^]
But the solution mentioned in that link is not clear to me.


Any ideas/suggestions?
Thanks in advance.

推荐答案

解决方案1可以解决问题。但是,由于这是在析构函数中,而你所要做的就是清理内存,我会坚持使用前向迭代器并简单地删除对erase的调用。如果你想对它整洁,你最后可以称之为清除。因此:

Solution 1 may solve the issue. However, since this is in the destructor and all you're trying to do is clear up the memory, I'd stick with the forward iterator and simply remove the call to "erase". If you want to be neat about it, you could call "clear" at the end. Thus:
~CMyClass()
{
 // clear map
 for (CSourceToIdMap::iterator itEvent = m_EventMap.begin();
      itEvent != m_EventMap.end(); ++itEvent)
 {
   delete itEvent->second;
 }
 m_EventMap.clear();  // not necessary, but for neatness
}



问候,

Ian。


Regards,
Ian.


好的,我编译并运行良好,没有运行时错误(作为Win32控制台) CPanel和CIdToTargetMap的一些虚拟类的应用程序。



我唯一能想到的是你有一个不同定义的CSourceToIdMap的早期声明。尝试将类中使用的类型的名称更改为其他名称(例如CSourceToIdMap2)以查看是否有效并修复(并因此确认)问题。



一另一种可能性是包含的顺序 - 也许有一些东西干扰了地图。尝试将包含数量减少到最低要求,并按顺序播放。 (如果包含错误的顺序,某些Windows包含对STL的破坏)。



您使用的是什么版本的STL?



问候,

Ian。
Okay, I compiled and ran this fine with no runtime error (as a Win32 console application with some dummy classes for CPanel and CIdToTargetMap).

The only thing I can think of is that you have an earlier declaration of "CSourceToIdMap" with a different definition. Try changing the name of the type used within the class to something else (e.g. CSourceToIdMap2) to see if that works and fixes (and therefore confirms) the problem.

One other possibility is the order of the includes - perhaps there is something interfering with "map". Try reducing the number of includes to the bare minimum required, and play with the order of them. (Some of the Windows includes play havoc with the STL ones if they are included in the wrong order).

What version of the STL are you using?

Regards,
Ian.






擦除集合中的项目时,前向迭代器无效。你想要做的是使用反向迭代器从列表末尾删除你的集合。您将从rbegin()迭代到rend()...
[edit: I just realized you said it was a runtime error, not compile error...]

When you erase an item in a collection, the forward iterator is invalidated. What you want to do is delete your collection from the end of list to front using a reverse iterator. You would iterate from rbegin() to rend()...


这篇关于STL:RunTime错误:map / set迭代器不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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