c++ - 遍历 map 的 map 和删除项目的最佳模式是什么?

标签 c++ stl iterator

我正在编写一些基本上可以执行以下操作的代码:

mapSize = map.size();
for(iter=map.begin;iter!=map.end();)
{
  call function which might delete a map item;
  if(map.size()==mapSize )
  {
     iter++;
  }
  else
  {
     mapSize = map.size();
     iter=map.begin(); /* Start again if something was deleted */
  }
}

我认为一定有更好的方法来做到这一点。有什么建议吗?

最佳答案

该函数应该为您返回下一个有效的迭代器。这就是 map 的正常 erase 功能的工作原理。

关于c++ - 遍历 map 的 map 和删除项目的最佳模式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6200989/

相关文章:

python - 单次迭代共享迭代器

c++ - "thread-local storage not supported for this target",适合#ifdef?

c++ - 如何使用 SQLite PRAGMA 线程 = X ;转换为 C++ 代码

c++ - 如何制作不同常量的元组?

c++ - 带有自定义分配器的 std::string

Java:实现通用迭代器

c++ - 使用迭代器访问存储在 vector 中的类对象的方法。如何?

c++ - 我在 Clang 和 GCC 中发现了错误吗?

c++ - 如何向现有类数组插入新值? (C++)

c++ - 如何使用 bind1st 和 bind2nd?