20

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

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

有没有人有这方面的经验或者可以指出我正确的页面?

4

2 回答 2

18

一切都在文档中...... 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;
   }
}
于 2012-08-29T09:54:15.943 回答
12

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

对于第二部分(访问一个双向映射,它是一个方向的映射和另一个方向的多映射)这是不正确的。

正确的访问方式是 [ 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
于 2013-04-13T14:59:11.377 回答