c – 在Boost的多索引容器中获取非const迭代器

使用Boost 1_33_1,我得到一个错误,暗示我的迭代器是一个const迭代器(因为它不会让我从find()中得到结果).
$g++ bmi_iter_tst.cpp 
bmi_iter_tst.cpp: In function ‘void tst(employee_set&)’:
bmi_iter_tst.cpp:32: error: invalid initialization of reference of type ‘employee&’ from expression of type ‘const employee’

我知道我不应该修改任何键值,但我没有,但我仍然需要非const访问sto修改容器元素中的其他数据.

我知道我已经在其他地方成功完成了这项工作,我只是看不出是什么会使这个const.

下面的代码源自原始的boost :: multi_index示例

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>


using boost::multi_index_container;
using namespace boost::multi_index;

struct employee
{
  int         id;
  int         tst;

  employee(int id_):id(id_),tst(0){}
};


struct id{};


typedef multi_index_container<
  employee,indexed_by<
    ordered_unique<
      tag<id>,BOOST_MULTI_INDEX_MEMBER(employee,int,id)> >
> employee_set;


void tst(employee_set& s)
{
  employee_set::index_iterator<id>::type it = s.get<id>().find(11);
  employee& eref = *it;

  eref.tst++;
}

解决方法

来自 random access indices上的MultiIndex文档:

As usual in Boost.MultiIndex,elements of random access indices are
immutable and can only be modified through member functions replace
and modify. This precludes the usage of many mutating algorithms that
are nonetheless applicable to std::vectors.

这也适用于ordered indexes.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...