multimap-swap


//      2018/05/06 9:39:32
//      multimap-swap

// exchage two maltimap
#include <iostream>
#include <map>
#include <list>
#include <numeric>
#include <algorithm>
#include <iterator>

using namespace std;

typedef multimap<int, list<int>> M;

void print(M m){
    M::iterator It = m.begin();
    list<int>::iterator Li;
    while (It != m.end()){
        cout << "key:" << It->first
            << ";value:";
        for (Li = It->second.begin(); Li != It->second.end(); Li++){
            cout << *Li << " ";
        }
        It++;
    }
    cout << endl;
}

//-------------------------------
int main(){
    list<int> L1, L2;
    L1.push_back(1);
    L1.push_back(2);
    L1.push_back(3);

    L2.push_back(5);
    copy(L1.begin(), L1.end(), back_inserter(L2));

    M m1, m2;
    m1.insert(M::value_type(1,L1));
    m2.insert(M::value_type(2,L2));

    cout << "multimap m1:" << endl;
    print(m1);

    cout << "multimap m2:" << endl;
    print(m2);

    if (m1 == m2){
        cout << "multimaps m1 and m2 are equal." << endl;
    }
    else{
        cout << endl << "After m1.swap(m2)" << endl;
        m1.swap(m2);
        cout << "multimap m1:" << endl;
        print(m1);
        cout << "multimap m2:" << endl;
        print(m2);
    }
    return 0;
}


/*
OUTPUT:
    multimap m1:
    key:1;value:1 2 3
    multimap m2:
    key:2;value:5 1 2 3

    After m1.swap(m2)
    multimap m1:
    key:2;value:5 1 2 3
    multimap m2:
    key:1;value:1 2 3
*/ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值