C++ STL multimap用法总结

                                                      C++ STL multimap用法总结


/

/*/

Author: NYIST_zsj
Date:2014/12/
promble: multimap
主要操作:
          1、遍历整个multimap
          2、遍历某个特定的键
          3、查找某个特定的键
          4、删除某个特定的键和特定的值
          
///*/

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
/

multimap<int,int,greater<int> > mump;  //键值从大到小 less<Type> 键值从小到大
typedef multimap<int,int>::iterator Iterator;
int main()
{
    mump.insert(make_pair(1,2));
    mump.insert(make_pair(1,4));
    mump.insert(make_pair(1,5));
    mump.insert(make_pair(1,6));
    mump.insert(make_pair(3,4));
    mump.insert(make_pair(3,5));
    mump.insert(make_pair(3,4));

   //遍历整个multimap

    cout << "---------------visit all multimap------------ " << endl;
    Iterator it;
    it = mump.begin();
    for(it = mump.begin();it != mump.end();++it) {
        cout << it->first << "  " << it->second << endl;
    }

     cout << "----------------- End --------------------------- " << endl;
    //单独遍历只有涉及某个key, equal_range(key)

    cout << endl << "-----------------visit only one key multimap--------------- " << endl;
    pair<Iterator,Iterator> it2;
    it2 = mump.equal_range(1);
    /*
       equal_range()返回的是一个pair,该pair的存放的是两个迭代器
       equal_range().first 存放的迭代器指向的是起始位置
       equal_range().second 存放的迭代器指向的是末尾的位置
       有点类似于map中的begin()/end()
    */

    for(it = it2.first;it != it2.second;++it) {
        cout << it->first << " " << it->second << endl;

    }
    cout << " ------------------- End ----------------- " << endl;

    //删除某个键值,某个值

     cout << " -----------Delete only one key and only one value ------------- " << endl;
     it2 = mump.equal_range(1);
     for(it = it2.first;it != it2.second; ++it) {
        if(it->first == 1 && it->second == 5) {
            cout << "delete: " << it->first << " " << it->second << endl;
            mump.erase(it);
        }
     }

      it = mump.begin();
      for(;it != mump.end();++it) {
        cout << it->first << " " << it->second << endl;
      }
     cout << "-------------------- End --------------------------- " << endl;

    //删除某个键值
    cout << endl << "-------------Delete only one key------------ " << endl;
    it = mump.find(1);
    cout << " delete 3 after " << endl;
    mump.erase(3);
    it = mump.begin();
    for(;it != mump.end();++it) {
        cout << it->first << " " << it->second << endl;
    }
    cout << "-------------End----------------" << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值