map insert()两种用法

#include <cstdlib>
#include <iostream>
#include <map>
#include <utility>
using namespace std;
int main(int argc, char *argv[])
{
    map<int, int> m;
    
    typedef map<int, int>::value_type vt;
    
    for(int i=0; i<50; i++)
    {
        int val = rand() % 1000;
        pair<map<int, int>::iterator, bool> ret;
        
        //1th insert mathod
        ret = m.insert(vt(val, i));
        if(!ret.second)
            cout << "insert key: " << val << "failed" << endl;
              
        //2th insert mathod
        val = rand() % 1000;
        ret = m.insert(make_pair(val, i));
        if(!ret.second)
            cout << "insert key: " << val << "failed" << endl;
    }
    
    map<int, int>::iterator it = m.begin();
    for(it; it != m.end(); it++)
        cout << it->first << " ";
    cout << endl;
        
    system("PAUSE");
    return EXIT_SUCCESS;
}

posted on 2017-04-20 09:04  wgwyanfs  阅读(749)  评论(0编辑  收藏  举报

导航