zoukankan      html  css  js  c++  java
  • map-erase

    ////////////////////////////////////////
    //      2018/05/01 14:31:00
    //      map-erase
    
    // removes elements
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    typedef map<string, int, less<string>> M;
    
    void print(M & m){
        M::iterator it = m.begin();
        cout << "map:" << endl;
        while (it != m.end()){
            cout << it->first << "-" << it->second << endl;
            it++;
        }
    }
    int main(){
    
        typedef M::value_type v_t;
    
        M m;
        m.insert(v_t("AAA",1));
        m.insert(v_t("BBB",2));
        m.insert(v_t("CCC",3));
        m["DDD"] = 4;
        m["EEE"] = 5;
    
        print(m);
    
        // remove element with key 'BBB'
        m.erase("BBB");
        print(m);
    
        M::iterator it;
        it = m.find("DDD");
        //remove element pointed by it
        m.erase(it);
        print(m);
    
        it = m.find("CCC");
        // remove the range if element
        // 包前不包后
        m.erase(m.begin(),++it);
    
        print(m);
    
        return 0;
    }
    
    /*
    OUTPUT:
        map:
        AAA-1
        BBB-2
        CCC-3
        DDD-4
        EEE-5
        map:
        AAA-1
        CCC-3
        DDD-4
        EEE-5
        map:
        AAA-1
        CCC-3
        EEE-5
        map:
        EEE-5
    */
  • 相关阅读:
    05391
    05390
    05389
    05388
    1006 Sign In and Sign Out (25分)
    1002 A+B for Polynomials (25分)
    1005 Spell It Right (20分)
    1003 Emergency (25分)
    1001 A+B Format (20分)
    HDU 2962 Trucking
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12537857.html
Copyright © 2011-2022 走看看