OPERATOR_BRACKET_IS_NOT_SUPPORTED 在 boost::bimap

OPERATOR_BRACKET_IS_NOT_SUPPORTED on boost::bimap

我试图在 boost::bimap 上使用括号运算符但没有成功。

对于我要解决的问题,我需要一个满足以下要求的bimap

这导致我为我的 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

一如既往,我们指的是the documentation:

set_of and unordered_set_of map views overload operator[] to retrieve the associated data of a given key only when the other collection type is a mutable one. In these cases it works in the same way as the standard.

您在右侧使用的是非可变类型:

Side collection type       Dereferenced data
--------------------------------------------
set_of                     constant
multiset_of                constant
unordered_set_of           constant
unordered_multiset_of      constant
list_of                    mutable
vector_of                  mutable
unconstrained_set_of       mutable

因此,您不能使用 operator[]

错误消息说的是:

OPERATOR_BRACKET_IS_NOT_SUPPORTED