boost::bimap

// 忽略警告
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)

#include <iostream>
#include <boost/bimap.hpp>// map的双向映射
#include <boost/typeof/typeof.hpp>// BOOST_AUTO
#include <boost/bimap/unordered_set_of.hpp>// unordered_set_of
using namespace boost;
using namespace std;
using namespace boost::bimaps;
int main()
{
    bimap<int,string> bmp;//   默认为<int,set_of>相当于std::map

    // 左视图left相当于map<int,string> // 按int有序
    bmp.left.insert(make_pair(1, "one"));
    bmp.left.insert(make_pair(1, "one1"));    // 1已经存在,插入无效
    bimap<int, string>::value_type vt(2, "two");
    bmp.insert(vt);
    bmp.left.insert(make_pair(5, "two"));    // two已经存在,插入无效
    BOOST_AUTO(it, bmp.left.begin());
    while (it != bmp.left.end())
    {
        cout << "key:" << it->first << "    value:" << it->second << endl;// 1   2
        it++;
    }

    // 右视图right相当于map<string,int>// 按string有序
    bmp.right.insert(make_pair("three",3));
    bmp.right.insert(make_pair("one1111", 1));    // 1已经存在,插入无效
    bmp.right.insert(make_pair("one", 11));        // one已经存在,插入无效
    bmp.right.insert(make_pair("four", 4));
    BOOST_AUTO(it1, bmp.right.begin());
    while (it1 != bmp.right.end())
    {
        cout << "key:" << it1->first << "    value:" << it1->second << endl;//foue one three two
        it1++;
    }

    cout << "-----------------unordered_set_of-------------------" << endl;
    bimap<int, unordered_set_of<string>> bmp1;// 右视图相当于std::unordered_map
    bimap<int, string>::value_type vt1(2, "two");
    bmp1.insert(vt);
    bmp1.left.insert(make_pair(1, "one"));
    bmp1.left.insert(make_pair(4, "four"));
    bmp1.left.insert(make_pair(3, "three"));

    BOOST_AUTO(it2, bmp1.right.begin());
    while (it2 != bmp1.right.end())
    {
        cout << "key:" << it2->first << "    value:" << it2->second << endl;// two one four three
        it2++;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值