【问题标题】:Boost::Bimap equivalent of bidirectional multimapBoost::Bimap 等价于双向多图
【发布时间】:2012-08-29 09:41:41
【问题描述】:

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

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

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

【问题讨论】:

    标签: c++ boost multimap bimap


    【解决方案1】:

    一切都在文档中...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;
       }
    }
    

    【讨论】:

      【解决方案2】:

      对于您问题的第一部分(如何定义双向多图?),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
      

      【讨论】:

      • 我不确定您所说的“似乎有效”是什么意思。您得到的输出是因为它和 r_it 使用 find() 初始化到多图中间的某个位置。如果用 begin() 初始化,所有三对都可以正确打印。
      • 是的,但这里的重点不是打印三对。关键是访问 bimap(从左侧和/或从右侧)并检索给定键的对。我已经编辑了我的回复,包括一个示例,您可以使用 equal_range 来实现这一点。我希望这会有所帮助。
      • @JavierBravo 感谢更新此答案。然而,确实你完全错过了情节:Live On Coliru
      • 这里是 equal_range 示例的简化版本:Live On Coliru /cc @namezero 我想我们可以认为你搞砸了迭代器处理,甚至没有在一年多的时间里看到错误,有足够的理由理解为什么将复杂性排除在呼叫站点之外很重要:)
      • @sehe 哇,我根本不记得这个帖子了... 未经测试:不应该是 boost::bimap<:multiset_of>, bimaps::multiset_of >工作吗? *编辑:再次阅读原始问题;一个方向的“简单”地图是一项要求。
      猜你喜欢
      • 2012-05-31
      • 2020-11-17
      • 2012-11-03
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多