使用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’
Run Code Online (Sandbox Code Playgroud)
我知道我不应该修改任何键值,但我没有,但我仍然需要非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++;
}
Run Code Online (Sandbox Code Playgroud)
来自关于随机访问索引的MultiIndex 文档:
与 Boost.MultiIndex 中一样,随机访问索引的元素是不可变的,只能通过成员函数replace 和modify 进行修改。这排除了许多仍然适用于 std::vector 的变异算法的使用。
这也适用于有序索引。
| 归档时间: |
|
| 查看次数: |
3594 次 |
| 最近记录: |