gpt4 book ai didi

c++ - OPERATOR_BRACKET_IS_NOT_SUPPORTED 在 boost::bimap 上

转载 作者:太空狗 更新时间:2023-10-29 21:14:48 26 4
gpt4 key购买 nike

我试图在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com