我遇到问题" map / set迭代器不可递增"与我的multimap相关的错误。我试过谷歌搜索答案,但答案并没有帮助我。我假设问题是因为我的代码的第一部分执行了" akcja"命令,可能(但不是必须)删除multimap的一个组件:
while ((c = getch()) != 27)
{
if (c == 'n')
{
typedef multimap<int, Organizm*>::iterator iterator;
for (int i = 10; i>= 0; i--)
{
std::pair<iterator, iterator> iterpair = kolejnoscRuchu.equal_range(i);
iterator it = iterpair.first;
for (; it != iterpair.second; ++it)
{
if(it->second->inicjatywa !=0)
{
it->second->akcja();
}
}
}
}
如果满足某些条件,akcja()将触发删除元素的命令:
void Swiat::usunOrganizm(Organizm *organizm)
{
this->organizmy[organizm->pozycja.x][organizm->pozycja.y] = NULL;
typedef multimap<int, Organizm*>::iterator iterator;
std::pair<iterator, iterator> iterpair2 = this->kolejnoscRuchu.equal_range(organizm->inicjatywa);
iterator it2 = iterpair2.first;
for (; it2 != iterpair2.second; ++it2)
{
if (it2->second == organizm)
{
cout << "usuwam " << it2->second->rysowanie() << endl;
kolejnoscRuchu.erase(it2);
delete organizm;
break;
}
}
}
我添加了&#34; cout&lt;&lt; &#34; usuwam&#34; &LT;&LT; it2-&gt; second-&gt; rysowanie()&lt;&lt; ENDL;&#34;部分以确认从我的multimap中删除任何元素后是否发生错误。我将不胜感激任何帮助
答案 0 :(得分:2)
如果您删除it2
处的元素,则无法再使用it2
。增加它将不再可能。
您可以轻松编写一个迭代循环来容忍循环控件本身的擦除:
iterator it = iterpair2.first;
while (it != iterpair.second)
{
iterator next_it = it;
++next_it;
/* it's possible for it to be deleted here */
it = next_it;
}
但是,如果在循环体中删除next_it
,则上述操作将失败。所以,如果你想要更一般,你需要一个明确的比较:
while (it != iterpair.second)
{
iterator next_it = it;
++next_it;
/* ... */
if (some_condition)
{
/* Need to erase it_to_erase */
if (it_to_erase == next_it) ++next_it;
theMap.erase(it_to_erase);
}
/* ... */
it = next_it;
}
即使这需要迭代的代码也是擦除元素的代码。如果擦除代码不相关(例如,因为它在迭代中调用的函数中),那么实际上没有允许立即擦除的解决方案。
因此,在这种情况下,您需要实现某种形式的延迟擦除。在OP中呈现的特定情况中,映射的mapped_type
是一个其值不能为null的指针,一种简单的延迟擦除形式是简单地将要擦除的元素的映射值设置为0。
在下面的简单延迟擦除方案中,我假设外环本身可以自由擦除元素;也就是说,它不在迭代期间调用的函数中。为简单起见,我使用了一些C + 11功能。
/* Outer loop */
for (auto it = myMap.begin(), end = myMap.end();; ++i) {
/* Erase any previously marked elements */
while (it != end && !it->second) it = myMap.erase(it);
if (it == end) break;
/* Do something with this element */
/* ... */
/* This function might "delete" arbitrary elements. See below */
secondProcess(myMap);
/* At this point, "it" might not be valid, so you would need to
check before trying to use it.
*/
}
这是内在的功能:
void secondProcess(MapType& myMap) {
/* ... */
for (auto it2 = myMap.begin(), end = myMap.end(); it2 != end; ++it2) {
if (it2->second) { /* Make sure the element is not already marked */
/* ... */
/* Here we've decided to erase the element at "it2"
* The delete comes from the code in OP; it is only OK if the
* mapped value is the only pointer to the object it points to.
*/
delete it2->second;
it2->second = 0;
/* Since we don't actually erase it2, no other adjustment
* is needed
*/
}
}
}