26

我有一个像这样的bimap:

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<A, B>对象的迭代器。那么这个构造函数对这种 bimap 有什么期望呢?

4

4 回答 4

29

我使用以下“工厂函数”,它接受一个大括号初始化列表并返回一个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}});
于 2015-08-05T19:43:06.810 回答
16

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

例子:

#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
     */
}
于 2016-03-11T07:36:41.170 回答
10

迭代器开始/结束应该用于一系列 bimap 值。

boost::bimap< A, B>::value_type

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

好的,这是一个为我编译和运行的示例(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;
}
于 2013-11-29T16:57:10.023 回答
2

这留下了要清理的向量,这在某些情况下可能是一个问题。这里有一个简短的帮助类,也可以解决您的问题。由于类实例是临时的,因此无论在何处使用它都会立即清理。这是基于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);
于 2014-01-04T08:35:19.783 回答