c++ - boost multi_index_container 更改键 -> 容器状态不正确

标签 c++ boost dictionary key boost-multi-index

假设我们有一个多索引容器:

#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/tag.hpp>
#include <boost/multi_index/key_extractors.hpp>

struct A{
A(int i){id=i;}
    int id;
};

typedef boost::multi_index::multi_index_container<
    A * ,
    boost::multi_index::indexed_by<
        boost::multi_index::random_access<
            boost::multi_index::tag<by_insertion>
        >, // this index represents insertion order
        boost::multi_index::hashed_unique<
            boost::multi_index::tag<by_id>,
            boost::multi_index::member<A, int, &A::id>
        >
    >
> MapType;

MapType map;

map.get<1>().insert(new A(1));
map.get<1>().insert(new A(2));

(*map.get<1>().find(1))->id=4; // HERE IF I CHANGE THE KEY, I CAN NOT FIND either key=4 or 1

MapType::nth_index<1>::type::iterator it = map.get<1>().find(4);
if(it != map.get<1>().end() ){
    std::cout << "FOUND A:" << *it << std::endl;
} // DOES NOT WORK?? WHY CANT I FIND the ELement with Key 4?

现在的问题是我可能设置了 boost::multi_index::member<A, int, &A::a>错误是因为当我更改一些 key 时。我找不到 key = 4 的元素?

这里用错了什么? 非常感谢任何帮助!

最佳答案

不,散列索引不存储散列值,它们总是即时计算。为什么要更改 key ,但仍要按旧值查找?

关于c++ - boost multi_index_container 更改键 -> 容器状态不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17857279/

相关文章:

c++ - C++ 标准库是 C++ 语言的一部分吗?

c++ - 代码 C++ 中的动态多态性错误

python - 如何在 Python 中对字典中的值进行排序?

c# - 我应该在访问字典之前检查特定键是否存在于字典中吗?

c++ - 异步tcp套接字和发送数据时的进度

c++ - 奇怪的 vector<long> 添加 '0' 作为 '-1'

c++ - std::vector 可视化工具在 std::vector<boost::variant> 上无法正常工作

python - 将多态对象从 C++ 传递给 python 函数

c++ - 链接 boost - 对 `boost::serialization::singleton_module::get_lock()' 的 undefined reference

python - 如何在python中将字典项作为函数参数传递?