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
*/
原文地址:https://www.cnblogs.com/laohaozi/p/12537857.html