zoukankan      html  css  js  c++  java
  • C++ std::map的安全遍历并删除元素的方法

    首先我们讲遍历std::map, 大部分人都能写出第一种遍历的方法,但这种遍历删除的方式并不太安全。

    第一种 for循环变量:

    #include<map>
    #include<string>
    #include<iostream>
    using namespace std;
    
    int main()
    {
        map<int,string*> m;
        m[1]= new string("1111111111111111");
        m[2]= new string("2222222222222222");
        m[3]= new string("3333333333333333");
        m[4]= new string("4444444444444444");
        m[0]= new string("5555555555555555");
        map<int,string*>::iterator it;
        for(it=m.begin();it!=m.end();++it)
        {
            cout<<"key: "<<it->first <<" value: "<<*it->second<<endl;
            delete it->second;
            m.erase(it);
        }
    return   0;
    }
    

    结果如下:

    key: 0 value: 5555555555555555
    key: 1 value: 1111111111111111
    key: 2 value: 2222222222222222
    key: 3 value: 3333333333333333
    key: 4 value: 4444444444444444
    

    第二种while循环的遍历:

    #include <map>
    #include <string>
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    struct ltstr
    {
      bool operator()(const char* s1, const char* s2) const
      {
        return strcmp(s1, s2) < 0;
      }
    };
    
    int main()
    {
      map<const char*, int, ltstr> ages;
      ages["Homer"] = 38;
      ages["Marge"] = 37;
      ages["Lisa"] = 8;
      ages["Maggie"] = 1;
      ages["Bart"] = 11;
    
      while( !ages.empty() ) {
        cout << "Erasing: " << (*ages.begin()).first << ", " << (*ages.begin()).second << endl;
        ages.erase( ages.begin() );
      }
    
    }
    

    运行结果:

    Erasing: Bart, 11
    Erasing: Homer, 38
    Erasing: Lisa, 8
    Erasing: Maggie, 1
    Erasing: Marge, 37
    

    第三种更安全的for 循环遍历:

    #include<map>
    #include<string>
    #include<iostream>
    using namespace std;
    
    int main()
    {
        map<int,string*> m;
        m[1]= new string("1111111111111111");
        m[2]= new string("2222222222222222");
        m[3]= new string("3333333333333333");
        m[4]= new string("4444444444444444");
        m[0]= new string("5555555555555555");
        map<int,string*>::iterator it;
        for(it=m.begin();it!=m.end();)
        {
            cout<<"key: "<<it->first <<" value: "<<*it->second<<endl;
            delete it->second;
            m.erase(it++);
        }
        return   0;
    }
    

    运行结果与第一种方式相同,不过这种删除方式也是STL源码一书中推荐的方式,分析 m.erase(it++)语句,map中在删除iter的时候,先将iter做缓存,然后执行iter++使之指向下一个结点,再进入erase函数体中执行删除操作,删除时使用的iter就是缓存下来的iter(也就是当前iter(做了加操作之后的iter)所指向结点的上一个结点)。

    根据以上分析,可以看出(m.erase(it++) )和(m.erase(it); iter++; )这个执行序列是不相同的。前者在erase执行前进行了加操作,在it被删除(失效)前进行了加操作,是安全的;后者是在erase执行后才进行加操作,而此时iter已经被删除(当前的迭代器已经失效了),对一个已经失效的迭代器进行加操作,行为是不可预期的,这种写法势必会导致 map操作的失败并引起进程的异常。

  • 相关阅读:
    FJNU 1151 Fat Brother And Geometry(胖哥与几何)
    FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术)
    FJNU 1159 Fat Brother’s new way(胖哥的新姿势)
    HDU 3549 Flow Problem(最大流)
    HDU 1005 Number Sequence(数列)
    Tickets(基础DP)
    免费馅饼(基础DP)
    Super Jumping! Jumping! Jumping!(基础DP)
    Ignatius and the Princess IV(基础DP)
    Keywords Search(AC自动机)
  • 原文地址:https://www.cnblogs.com/muahao/p/9299658.html
Copyright © 2011-2022 走看看