【问题标题】:How to construct Boost bimap from static list?如何从静态列表构造Boost bimap?
【发布时间】:2013-12-15 20:00:50
【问题描述】:

我有一个这样的双图:

using MyBimap = boost::bimaps::bimap<
    boost::bimaps::unordered_set_of<A>,
    boost::bimaps::unordered_set_of<B>>;

我想从静态初始化列表构造它,因为它可以为 std::map 完成:

MyBimap map{{a1, b1}, {a2, b2}, {a3, b3}};

不幸的是,它不起作用,因为bimap 不支持初始化列表,所以我尝试了一种解决方法。 Boost 的文档列出了以下构造函数:

 bimap();

 template< class InputIterator >
 bimap(InputIterator first,InputIterator last);

 bimap(const bimap &);

所以我尝试了第二个,像这样:

std::vector<std::pair<A,B>> v{{a1, b1}, {a2, b2}, {a3, b3}};
MyBimap map(v.begin(), v.end());

它也没有工作。文档并不清楚这个构造函数需要什么样的迭代器,但显然它不仅仅是std::pair&lt;A, B&gt; 对象的迭代器。那么这个构造函数对这种 bimap 有什么期望呢?

【问题讨论】:

    标签: c++ boost c++11 boost-bimap


    【解决方案1】:

    我使用以下“工厂函数”,它接受一个大括号初始化列表并返回一个boost::bimap:

    template <typename L, typename R>
    boost::bimap<L, R>
    makeBimap(std::initializer_list<typename boost::bimap<L, R>::value_type> list)
    {
        return boost::bimap<L, R>(list.begin(), list.end());
    }
    

    用法:

    auto myBimap = makeBimap<int, int>({{1, 2}, {3, 4}, {5, 6}});
    

    【讨论】:

      【解决方案2】:

      这里是 C++ 初学者:您可以使用 boost::assign 来生成初始化。我找到了这个解决方案here。

      例子:

      #include <boost/bimap.hpp>
      #include <boost/assign.hpp>
      
      //declare the type of bimap we want
      typedef boost::bimap<int, std::string> bimapType;
      //init our bimap
      bimapType bimap = boost::assign::list_of< bimapType::relation >
      ( 1, "one"   )
      ( 2, "two"   )
      ( 3, "three" );
      
      //test if everything works
      int main(int argc, char **argv)
      {
          std::cout << bimap.left.find(1)->second << std::endl;
          std::cout << bimap.left.find(2)->second << std::endl;
          std::cout << bimap.left.find(3)->second << std::endl;
          std::cout << bimap.right.find("one")->second << std::endl;
          std::cout << bimap.right.find("two")->second << std::endl;
          std::cout << bimap.right.find("three")->second << std::endl;
      
          /* Output:
           * one
           * two
           * three
           * 1
           * 2
           * 3
           */
      }
      

      【讨论】:

      • 不是随意的初始化列表语法,这将是可取的,但就像一个魅力:-)
      【解决方案3】:

      迭代器的开始/结束应该是一个 bimap 值的序列。

      boost::bimap&lt; A, B&gt;::value_type

      bimap 值很像 std::pair,可以使用{a1, b1}syntax 进行初始化。它们中的一个向量似乎也可以工作,它为构造函数提供了可用的迭代器。

      好的,这是一个为我编译和运行的示例(gcc 4.8.2 --std=c++11)

      #include <vector>
      #include <boost/bimap.hpp>
      
      using namespace std;
      int main() {
          typedef boost::bimap< int, int > MyBimap;
      
          std::vector<MyBimap::value_type > v{{1, 2}, {3, 4}, {5, 6}};
      
          MyBimap M(v.begin(),v.end());
      
          std::cout << "The size is " << M.size()
                    << std::endl;
      
          std::cout << "An entry is 1:" << M.left.at(1)
                    << std::endl;
      }
      

      【讨论】:

        【解决方案4】:

        这会留下一个需要清理的向量,这在某些情况下可能是一个问题。这里有一个简短的帮助类,也可以解决您的问题。由于类实例是临时的,因此无论在何处使用它都会立即清理。这是基于https://stackoverflow.com/a/1730798/3103767

        // helper for bimap init (simple, lightweight version of boost::assign)
        template <typename T, typename U>
        class create_bimap
        {
            typedef boost::bimap< T, U > bimap_type;
            typedef typename bimap_type::value_type value_type;
        private:
            boost::bimap<T, U> m_map;
        public:
            create_bimap(const T& left, const U& right)
            {
                m_map.insert( value_type(left, right) );
            }
        
            create_bimap<T, U>& operator()(const T& left, const U& right)
            {
                m_map.insert( value_type(left, right) );
                return *this;
            }
        
            operator boost::bimap<T, U>()
            {
                return m_map;
            }
        };
        

        如下使用:

        boost::bimap<string,int> myMap = create_bimap<string,int>
            ("c",1)
            ("b",2)
            ("a",3);
        

        【讨论】:

          猜你喜欢
          • 2020-07-21
          • 2022-01-22
          • 2012-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-17
          • 2022-12-06
          相关资源
          最近更新 更多