如何修改提升多索引的只读元素
how to modify the read only elements of boost multi index?
我刚刚注意到我无法在提升多索引容器元素中进行更改。这是真的吗?(基于以下简化代码)查看"更新"功能:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
namespace sim_mob
{
enum TrafficColor
{
Red =1, ///< Stop, do not go beyond the stop line.
Amber = 2, ///< Slow-down, prepare to stop before the stop line.
Green = 3, ///< Proceed either in the forward, left, or right direction.
FlashingRed = 4, ///future use
FlashingAmber = 5, ///future use
FlashingGreen = 6 ///future use
};
//Forward declarations
class Link
{
public:
int aa;
};
//////////////some bundling ///////////////////
using namespace ::boost;
using namespace ::boost::multi_index;
typedef struct
{
sim_mob::Link *LinkTo;
sim_mob::Link *LinkFrom;
// ColorSequence colorSequence;
TrafficColor currColor;
} linkToLink;
typedef multi_index_container<
linkToLink,
indexed_by< // index
random_access<>,//0
ordered_non_unique< member<linkToLink,sim_mob::Link *, &linkToLink::LinkTo> >, // 1
ordered_non_unique< member<linkToLink,sim_mob::Link *, &linkToLink::LinkFrom> > // 2
>
> links_map;
class Phase
{
public:
typedef links_map::nth_index_iterator<1>::type LinkTo_Iterator;
Phase(double CycleLenght,std::size_t start, std::size_t percent): cycleLength(CycleLenght),startPecentage(start),percentage(percent){
};
void update(double lapse)
{
links_map_[0].currColor = Red;
}
std::string name;
private:
sim_mob::links_map links_map_;
};
}//namespace
int main()
{
sim_mob::Phase F(100,70,30);
}
无需经历整个程序。 请注意,在更新方法中,我收到此错误:multiIndex$ c++ exp2.cppexp2.cpp:在成员函数 'void sim_mob::P hase::update(double)' 中:exp2.cpp:69:29:错误:在只读对象中分配成员"sim_mob::链接到链接::currColor"
我刚刚在提升教程中读到迭代器只授予 const 访问权限。如何解决此问题?谢谢你的帮助
更改多索引容器中元素的三种不同方法。您可以使用links_map_.replace(iterator, new_value)
将现有元素替换为新元素。可以使用 links_map_.modify(iterator, unaryFunctionObject)
通过修饰符对象更改元素。最后,如果成员变量不是任何索引的一部分,则可以将其声明为 mutable
并直接更改它。在您的示例中,后者应该可以正常工作。
对于其他两种方法,请查看 Boost.MultiIndex 文档中的成员函数 replace
和 modify
的规范。
编辑:在您的特定情况下,mutable
的方法可能如下所示:
struct linkToLink
{
sim_mob::Link* LinkTo;
sim_mob::Link* LinkFrom;
// simplify write operations to this variable if used in a multi-index
// container - bypassing the const-ness. This is possible, because the
// variable is not part of any index.
mutable TrafficColor currColor;
};
相关文章:
- 数组索引的值没有增加
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 查找最接近的大于当前数字的数字的索引
- 在C++中调整向量中的索引
- 重载元组索引运算符-C++
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 为std::string的某个索引赋值
- 并行用于C++17中数组索引范围内的循环
- 非常量变量只读位置的赋值
- 是否可以在C++中使变量真正只读
- 跟随整数索引列表的自定义类迭代器
- 如何在for循环中包含两个索引值的测试条件
- D3D11-将混合权重和索引传递到顶点着色器
- 将转换字符键入 int 以用作向量C++的索引
- 在 C++ 中访问数组负索引处的内存不会返回垃圾
- 如何为圆环创建索引
- 在子集化后将包含索引号的列表列表映射到标准索引序列
- 查找字符在两个索引之间出现的次数
- 如何修改提升多索引的只读元素
- 为什么 QList::at() 不检查索引是否存在并返回只读值?