【问题标题】:OPERATOR_BRACKET_IS_NOT_SUPPORTED on boost::bimapOPERATOR_BRACKET_IS_NOT_SUPPORTED on boost::bimap
【发布时间】: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


【解决方案1】:

一如既往,我们参考the documentation

set_of 和 unordered_set_of 映射视图重载 operator[] 以仅在其他集合类型是可变集合类型时检索给定键的关联数据。在这些情况下,它的工作方式与标准相同。

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

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

【讨论】:

  • 谢谢,我更新了我的问题。如何声明 bimap 使得右侧是可变的?
  • @Stereo:我列出了可变视图。我建议放弃 operator[] 的梦想,只为您正在使用的类型使用受支持的 API。
猜你喜欢
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 2018-07-20
相关资源
最近更新 更多