boost之bimap(2)

来源:互联网 发布:java bind 参数 编辑:程序博客网 时间:2024/05/17 22:30

bimap

#include <iostream>#include <boost/assign.hpp>#include <boost/typeof/typeof.hpp>#include <boost/bimap.hpp>#include <boost/bimap/list_of.hpp>#include <boost/bimap/unordered_multiset_of.hpp>#include <boost/bimap/unordered_set_of.hpp>#include <boost/bimap/vector_of.hpp>#include <boost/bimap/set_of.hpp>#include <boost/bimap/multiset_of.hpp>#include <boost/bimap/unconstrained_set_of.hpp>#include <algorithm>using namespace std;using namespace boost;using namespace boost::bimaps;/////辅助函数template <typename T>void print_map(T &m){    for (BOOST_AUTO(pos, m.begin()); pos != m.end(); ++pos)    {        cout << pos->left << "------" << pos->right << endl;    }}/////辅助函数template <typename T>void print_map1(T &m){    for (BOOST_AUTO(pos, m.begin()); pos != m.end(); ++pos)    {        cout << pos->first << "------" << pos->second << endl;    }}int main(){    //////无序多值的bimap,左值和右值可以插入任意的可重复的值    bimap<unordered_multiset_of<int>, unordered_multiset_of<string>>bm;    bm.left.insert(make_pair(1,"111"));    bm.left.insert(make_pair(2,"222"));    bm.left.insert(make_pair(3,"333"));    bm.right.insert(make_pair("4444",4));    bm.right.insert(make_pair("5555",5));    bm.right.insert(make_pair("4444",6));    bm.right.insert(make_pair("7777", 3));    print_map(bm);    //////是错的    //for (BOOST_AUTO(pos, bm.begin()); pos != bm.end(); ++pos)    //{    //  cout << pos->first << "-" << pos->second << endl;    //}    bimap<set_of<int>, vector_of<string>>bm1;    bm1.left.insert(make_pair(1,"1111"));    bm1.left[2] = "2222";    //bm1.left.at(3) = "3333";    print_map(bm1);    //////是对的    /*for (BOOST_AUTO(pos, bm1.begin()); pos != bm1.end();++pos)    {        cout << pos->left <<"---"<< pos->right << endl;    }*/    //////使用标签类型,避免使用left和right    using namespace boost::bimaps;    bimap<tagged<int,struct id>,tagged<string,struct name>> bm2;    bm2.by<id>().insert(make_pair(1,"lijiajia"));    bm2.by<id>().insert(make_pair(2,"caoyanyan"));    bm2.by<name>().insert(make_pair("weiyimeng",3));    bm2.by<name>().insert(make_pair("hahahha",4));    print_map1(bm2.by<name>());    /////////使用assign    //typedef bimap<multiset_of<int>, vector_of<string>> bm_t;    //bm_t bm3 = assign::list_of(bm_t::relation)(1,"1111")(2,"2222");    //insert(bm3.left)(3,"3333")(4,"4444");    //push_back(bm3.right)("5555",5)("6666",6);    return 0;}
0 0