boost::multi_index的替代方法

Alternatives for boost::multi_index

本文关键字:方法 index multi boost      更新时间:2023-10-16

我想使用一些可以创建字典的东西,如

多键

Key1                     which will map to              SomeObject
Key2
Key3
Key4
etc

我想根据任意键查找。我对boost::multi_index有奇怪的问题,正在寻找替代方案。

我的编译器是Visual Studio 2005和我使用boost和不使用c++ 11。任何boost(除了multi_index)的东西都是非常受欢迎的。

当然,您应该解决您的奇怪问题,但这里有一个很好的技术:

std::vector<X> v; // elements of X in some order
std::vector<std::reference_wrapper<X const> > index1(v.begin(), v.end());
std::vector<std::reference_wrapper<X const> > index2(v.begin(), v.end());
// sort the indexes
std::sort(index1.begin(), index1.end(), by_property1);
std::sort(index2.begin(), index2.end(), by_property2);

当然,在发生变化的情况下保持同步并控制索引排序的运行时成本是一项稍微棘手的任务,这就是为什么—大多数时候—您希望multi_index_container

另外,请注意,为了更无忧无虑,您需要将vector替换为list,以享受迭代器/引用的稳定性。