c++ - OPERATOR_BRACKET_IS_NOT_SUPPORTED 在 boost::bimap 上

标签 c++ boost boost-bimap

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

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

  • left 排序,唯一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

最佳答案

一如既往,我们指的是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

关于c++ - OPERATOR_BRACKET_IS_NOT_SUPPORTED 在 boost::bimap 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39797219/

相关文章:

C++11 迭代交错数组?

c++ - 在静态方法中设置静态成员变量

c++ - 如何从 qml 访问 qabstractlistmodel 派生类对象作为另一个类的属性?

c++ - vector 复制值

c++ - 使用 UTF-8 在非 ASCII 字符上运行 Ascii 正则表达式

c++ - 使用 boost::regex (c++) 比较两个正则表达式

c++ - 使用 boost::shared_ptr 时出错

c++ - 将 boost::dynamic_bitset<> 插入 boost::bimap

c++ - 如何从静态列表构造Boost bimap?