首页 > 其他 > 详细

map insert()两种用法

时间:2017-04-20 10:41:54      阅读:321      评论:0      收藏:0      [点我收藏+]
#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;
}

map insert()两种用法

原文:http://www.cnblogs.com/wgwyanfs/p/6736914.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!