multimap-insert

////////////////////////////////////////
//      2018/05/04 17:12:45
//      multimap-insert

// insert elements into the maltimap

#include <iostream>
#include <map>
#include <string>

using namespace std;

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

    M m1, m2;
    char ch = 'A';
    for (int i = 0; i < 3; i++){
        m1.insert(v_t(i+1,ch+i));
        m2.insert(v_t(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
    5-E
    6-F
    6-F
*/
posted @ 2018-05-04 22:06  老耗子  阅读(66)  评论(0编辑  收藏  举报