zoukankan      html  css  js  c++  java
  • C++遍历中删除std::map元素

     

    在std::list中删除一个元素非常简单,直接使用erase方法即可,代码如下:

    for(iter = list.begin(); iter != list.end();) {
    if (shouldDelete(*iter))
    iter = list.erase(iter);
    else
    ++iter;
    }

    或者更简单点

    list.erase(std::remove_if(list.begin(), list.end(), shouldDelete), list_end());

    然而根据STL std::map中的定义void erase(iterator pos),此erase并不返回下一个元素的迭代器,因此不能采用std::list的方法

    The truth is that ‘erase’ in associative containers doesn’t invalidate any iterators except those that point to elements being erased (that’s also true for ’sid::list’). For this reason, you don’t really need ‘map::erase’ to return an iterator. Just do this

    for(iter = map.begin(); iter != map.end();) {
    if (shouldDelete(*iter))
    map.erase(iter++);
    else
    ++iter;
    }

    当然此方法同样也适合于std::list等

  • 相关阅读:
    置换笔记
    7.23
    Conveyor Belts
    CF #134 A~D
    Blocks && Fixing the Great wall
    Kakuro
    Deadlock Detection
    关于KeyFile的破解,含注册机源代码
    BMP位图之代码实现
    BMP位图之8位位图(三)
  • 原文地址:https://www.cnblogs.com/kex1n/p/2100787.html
Copyright © 2011-2022 走看看