map-insert

////////////////////////////////////////
//      2018/05/01 14:48:20
//      map-insert

// insert elements into the map
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(){
    typedef map<int, char, less<int>> M;
    typedef M::value_type v_t;

    M m1, m2;

    char ch = 'A';
    for (int i = 0; i < 3; i++){
        m1[i + 1] = ch + i;
        m2[i + 4] = ch + i + 3;
    }

    cout << "m1:" << endl;
    M::iterator it = m1.begin();
    while (it != m1.end()){
        cout << it->first << "-" << it->second << endl;
        it++;
    }

    cout << "m2:" << endl;
    it = m2.begin();
    while (it != m2.end()){
        cout << it->first << "-" << it->second << endl;
        it++;
    }

    // insert new element
    m1.insert(v_t(5,'E'));

    it = m2.find(6);
    // insert element pointed by it
    m1.insert(*it);

    cout << "m1:" << endl;
    it = m1.begin();
    while (it != m1.end()){
        cout << it->first << "-" << it->second << endl;
        it++;
    }

    // insert the range of elements
    m1.insert(m2.begin(), m2.end());
    cout << "m1:" << endl;
    it = m1.begin();
    while (it != m1.end()){
        cout << it->first << "-" << it->second << endl;
        it++;
    }

    return 0;
}

/*
OUTPUT:
    m1:
    1-A
    2-B
    3-C
    m2:
    4-D
    5-E
    6-F
    m1:
    1-A
    2-B
    3-C
    5-E
    6-F
    m1:
    1-A
    2-B
    3-C
    4-D
    5-E
    6-F
*/
原文地址:https://www.cnblogs.com/laohaozi/p/12537853.html