【发布时间】:2013-07-25 11:52:51
【问题描述】:
假设我们有一个多索引容器:
#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 = 4 的元素?
这里用错了什么? 任何帮助都非常感谢!
【问题讨论】:
-
等一下:您是否正在更改您在地图中查找的值,并期望地图神奇地知道这一点?通常,在不通知容器的情况下更改容器中的“正在查找的事物”会导致 UB。
标签: c++ boost map key boost-multi-index