std::list.erase(itor)在Android和Win32之间存在差异

std::list.erase(itor) have differences between Android and Win32

本文关键字:Win32 之间 Android 存在 erase list itor std      更新时间:2023-10-16

如果我在for循环中使用iterator,并且在迭代器的当前迭代中使用erase,我发现AndroidWIN32 之间存在一些差异

std::list<StackData*>::iterator itor;
std::list<StackData*>::iterator firstDelItor = __mStack.begin();
for(itor = __mStack.begin(); itor != __mStack.end();)
{
    firstDelItor = itor;
    PRINT_LOG(LOG_TAG, "A itor[%p] firstDelItor[%p]", *itor, *firstDelItor);
    itor = __mStack.erase(itor);
    PRINT_LOG(LOG_TAG, "B itor[%p] firstDelItor[%p]", *itor, *firstDelItor);
    ...
}

Android:erase(itor)之后,firstDelItor的值不变。

23766-23766 D/TAG: A itor[0x9b979fe0] firstDelItor[0x9b979fe0]
23766-23766 D/TAG: B itor[0x9b8e9dbc] firstDelItor[0x9b979fe0]

并且在WIN32上:firstDelItor的值变为0xdddddddd

TAG : A itor[0C6172D8] firstDelItor[0C6172D8]
Fatal at PRINT_LOG(LOG_TAG, "B itor[%p] firstDelItor[%p]", *itor, *firstDelItor); (itor = 0xcdcdcdcd, firstDelItor = 0xdddddddd)

因此,请解释在这种情况下AndroidWIN32之间的区别。

"对已擦除元素的引用和迭代器无效。"http://en.cppreference.com/w/cpp/container/list/erase