【发布时间】:2016-09-30 17:46:36
【问题描述】:
我试图在boost::bimap 上使用方括号运算符,但没有成功。
对于我要解决的问题,我需要一个满足以下要求的bimap:
-
左排序,唯一
int - 正确非唯一、非排序类型
这导致我为我的bimap 选择以下typedef,
typedef bimap<int, multiset_of<int> > bm;
我想在这种类型上使用方括号运算符但没有成功。这是我使用的代码,
#include <iostream>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
using namespace std;
using namespace boost::bimaps;
int main()
{
typedef bimap<int, multiset_of<int> > bm;
bm mapping;
mapping.insert(bm::value_type(1, 1));
mapping.insert(bm::value_type(2, 1));
mapping.insert(bm::value_type(3, 4));
for (auto it : {1 , 2, 3})
mapping.left[it] = it;
for (auto it : mapping.left)
cout << "{ " << it.first << ", " << it.second << " } ";
return 0;
}
这给了我一个很长的编译错误,其中基本位似乎是
/usr/include/boost/bimap/detail/map_view_base.hpp:351:9: error: no matching function for call to 'assertion_failed'
BOOST_BIMAP_STATIC_ERROR( OPERATOR_BRACKET_IS_NOT_SUPPORTED, (Derived));
(完整的现场示例和编译器输出可在以下位置找到:rextester)
我尝试了下面的解决方案,但它仍然产生错误,
#include <iostream>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
using namespace std;
using namespace boost::bimaps;
int main()
{
typedef bimap<int, multiset_of<int> > bm;
typedef bm::left_map map_type;
bm mapping;
map_type& m = mapping.left;
mapping.insert(bm::value_type(1, 1));
mapping.insert(bm::value_type(2, 1));
mapping.insert(bm::value_type(3, 4));
for (auto it : {1 , 2, 3})
m[it] = it;
for (auto it : mapping.left)
cout << "{ " << it.first << ", " << it.second << " } ";
return 0;
}
如何声明满足我的要求并支持括号运算符的bimap?
【问题讨论】:
-
我澄清了我的问题。感谢您的反馈
标签: c++ boost boost-bimap