zoukankan      html  css  js  c++  java
  • 正确使用STLMAP中Erase函数

    一切尽在代码中。

    #include <iostream>
    #include <map>
    #include <string>
    using namespace std ;
    
    int main(void) 
    { 
        map<int, string> m ;
        m.insert(pair<int, string>(1, "abc")) ;
        m.insert(pair<int, string>(2, "def")) ;
        m.insert(pair<int, string>(3, "def")) ;
        m.insert(pair<int, string>(4, "ghi")) ;
    
    
        map<int, string>::iterator itor ;
    
        // 错误的写法
        for (itor = m.begin(); itor != m.end(); ++itor)
        {
            if (itor->second == "def")
            {
                m.erase(itor) ; // map是关联式容器,调用erase后,当前迭代器已经失效
            }
        }
    
        // 正确的写法
        for (itor = m.begin(); itor != m.end();)
        {
            if (itor->second == "def")
            {
                m.erase(itor++) ; // erase之后,令当前迭代器指向其后继。
            }
            else
            {
                ++itor;
            }
        }
    
        // 另一个正确的写法,利用erase的返回值,注意,有些版本的stl-map没有返回值,比如SGI版,但vc版的有
        for (itor = m.begin(); itor != m.end();)
        {
            if (itor->second == "def")
            {
                itor = m.erase(itor) ; // erase的返回值是指向被删除元素的后继元素的迭代器
            }
            else
            {
                ++itor;
            }
        }
    
        // Print m
        map<int, string>::const_iterator citor ;
        for (citor = m.begin(); citor != m.end(); ++citor)
        {
            cout << citor->first << ":" << citor->second << endl ;
        }
    
        getchar() ; 
        return 0 ; 
    } 

    ==

  • 相关阅读:
    ActiveX控件的注册和反注册
    谷歌浏览器调用activex控件方法
    VC 中的ATL ActiveX 和 MFC ActiveX 有什么区别
    DirectX介绍(转)
    最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)(转)
    统计Visual Studio项目的代码行数
    【Sqlite3】sqlite_sequence表(转)
    linuxunix系统下的字符操作
    tif
    字符串截取
  • 原文地址:https://www.cnblogs.com/graphics/p/1771110.html
Copyright © 2011-2022 走看看