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

时间:2016-07-18 06:58:41

标签: c++ list

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

WIN32

Android:在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); ... } 之后,erase(itor)的值不变。

firstDelItor

在WIN32上:23766-23766 D/TAG: A itor[0x9b979fe0] firstDelItor[0x9b979fe0] 23766-23766 D/TAG: B itor[0x9b8e9dbc] firstDelItor[0x9b979fe0] 的值更改为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) Android之间的区别。

1 个答案:

答案 0 :(得分:4)

&#34;擦除元素的引用和迭代器无效。&#34; http://en.cppreference.com/w/cpp/container/list/erase