Boost :: Bimap相当于双向多图

时间:2012-08-29 09:41:41

标签: c++ boost multimap bimap

问题的第一部分是我正在尝试使用boost :: bimap,但是从文档中我不清楚如何定义双向多图。

问题的第二部分是我需要它是一个方向的地图和另一个方向的多个地图,可以使用boost :: bimap来完成吗?

有没有人经历过这个或者能指出我正确的页面?

2 个答案:

答案 0 :(得分:15)

所有内容都在文档中...... http://www.boost.org/doc/libs/1_51_0/libs/bimap/doc/html/boost_bimap/the_tutorial/discovering_the_bimap_framework.html

typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t;

实施例

#include <iostream>
#include <boost/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/multiset_of.hpp>

namespace bimaps = boost::bimaps;

int main()
{
   typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t;
   typedef bimap_t::value_type value_type;
   bimap_t bimap;
   bimap.insert(value_type(1, 1));
   bimap.insert(value_type(1, 2));
   auto& left = bimap.left;
   auto it = left.find(1);
   std::cout << "LEFT" << std::endl;
   for (; it != left.end(); ++it)
   {
      std::cout << it->first <<  " " << it->second << std::endl;
   }
   auto& right = bimap.right;
   auto r_it = right.find(2);
   std::cout << "RIGHT" << std::endl;
   for (; r_it != right.end(); ++r_it)
   {
      std::cout << r_it->first << " " << r_it->second << std::endl;
   }
}

答案 1 :(得分:9)

ForEverR的答案部分正确您的问题的第一部分(如何定义双向多图?)。

对于第二部分(访问bimap,它是一个方向上的地图和另一个方向上的多个地图)这是不正确的。

正确的访问方式是[http://rextester.com/BXBDHN12336]:

//bimap operations

#include <boost/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/multiset_of.hpp>

int main()
{
   typedef boost::bimap<boost::bimaps::multiset_of<int>, boost::bimaps::set_of<int>> bimap_t;
   typedef bimap_t::value_type value_type;
   bimap_t bimap;
   bimap.insert(value_type(1, 1));
   bimap.insert(value_type(10, 50)); 
   bimap.insert(value_type(1, 2));
   bimap.insert(value_type(9, 15));   

   typedef bimap_t::left_const_iterator l_itr_t;
   typedef std::pair<l_itr_t,l_itr_t> l_itr_range_t;

   l_itr_range_t ii = bimap.left.equal_range(1);

   std::cout << "LEFT" << std::endl;        
   for(l_itr_t it = ii.first; it != ii.second; ++it)
   {
     std::cout << "Key = " << it->first << "    Value = " << it->second << std::endl;
   }  

   std::cout << "RIGHT" << std::endl;
   std::cout << "Key = " << 1 << "    Value = " << bimap.right.at(1) << std::endl;
}

stdout:
LEFT
Key = 1    Value = 1
Key = 1    Value = 2
RIGHT
Key = 1    Value = 1

来自ForEverR的示例'似乎'因为插入数据的顺序而工作但是当你在结尾处插入另一对时检查结果bimap.insert(value_type(9,15));:

#include <iostream>
#include <boost/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/multiset_of.hpp>

namespace bimaps = boost::bimaps;

int main()
{
   typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t;
   typedef bimap_t::value_type value_type;
   bimap_t bimap;
   bimap.insert(value_type(1, 1));
   bimap.insert(value_type(1, 2));
   bimap.insert(value_type(9, 15));
   auto& left = bimap.left;
   auto it = left.find(1);
   std::cout << "LEFT" << std::endl;
   for (; it != left.end(); ++it)
   {
      std::cout << it->first <<  " " << it->second << std::endl;
   }
   auto& right = bimap.right;
   auto r_it = right.find(2);
   std::cout << "RIGHT" << std::endl;
   for (; r_it != right.end(); ++r_it)
   {
      std::cout << r_it->first << " " << r_it->second << std::endl;
   }
}

stdout: 
LEFT
1 1
1 2
9 15
RIGHT
2 1
15 9