可能有多种键类型的关联数组?

Fir*_*cer 3 c++ containers associative-array

我有一大堆对象(可能是1000个),我需要将它存储在一个容器中.我需要能够以两种方式找到特定的实例,通过它的ID号(64位无符号整数)或其名称(std :: string).通常,ID将是最常见的,但在某些情况下,名称是已知的,但不是ID.

std :: map可以提供单个< - >值,但是我不确定是否有两组std :: map容器,一个用于ID,另一个用于字符串是最好的方法.

编辑 - 修改过的代码和错误:

好吧,我想我会尝试多索引,因为我还有提升,但是我似乎无法编译它,即使我已经完成了与文档完全相同的功能,据我所知:(

测试代码:

namespace common
{
    class MyBaseClass
    {
    public:
        typedef boost::uint64_t Id;

        //name and id are constant, at least for the period im intrested in
        //when I want it in the container...
        const std::string &getName()const{return name;}
        Id getId()const{return id;}

        ...other stuff...
    };
}

class MyClass : public common::MyBaseClass
{
    ...other stuff...
};

typedef boost::multi_index_container
<
    MyClass*,
    boost::indexed_by
    <
        boost::ordered_unique<boost::const_mem_fun<MyBaseClass, MyBaseClass::Id,    &MyBaseClass::getId  > >,
        boost::ordered_unique<boost::const_mem_fun<MyBaseClass, const std::string&, &MyBaseClass::getName> >
    >
>MyClassList;
Run Code Online (Sandbox Code Playgroud)

和你的平均提升模板错误......

c:\ lib\c ++\boost\boost\aligned_storage.hpp(69):错误C2872:'detail':模糊符号
可以是'boost :: detail'
或'boost :: multi_index :: detail'c
:\ lib\c ++\boost\boost\multi_index\detail\index_node_base.hpp(42):参见
使用
[
size_ = 4,
alignment_ = 4
]
c:\ lib\c ++\boost\编译的类模板实例化'boost :: aligned_storage'的引用boost\multi_index\detail\index_node_base.hpp(47):参见
使用
[
Value = MyClass*
] 编译的类模板实例化'boost :: multi_index :: detail :: pod_value_holder'c
:\ lib\c ++\boost\boost\multi_index\detail\ord_index_node.hpp(582):请参阅
使用
[
Value = MyClass*,
Allocator = std :: allocator
]
c:\ lib 编译的类模板实例化'boost :: multi_index :: detail :: index_node_base'的引用\ c ++\boost\boost\multi_index\ordered_index.hpp(137):参见
使用
[
Super = boost :: multi_index :: detail :: index_node_base 编译的类模板实例化'boost :: multi_index :: detail :: ordered_index_node' >
]
c:\ lib\c ++\boost\boost\multi_index\ordered_index.hpp(119):参见
使用
[
KeyFromValue = boost :: multi_index :: const_mem_fun,
Compare = std :: 编译的类模板实例化'boost :: multi_index :: detail :: ordered_index' less,std :: allocator >>,
SuperMeta = boost :: multi_index :: detail :: nth_layer <2,MyClass*,boost :: multi_index :: indexed_by>,boost :: multi_index :: ordered_unique >>,std :: allocator >,
TagList = boost :: mpl :: vector0,
Category = boost :: multi_index :: detail :: ordered_unique_tag
]
c:\ lib\c ++\boost\boost\multi_index_container.hpp(86):参见类模板实例化的引用'
使用
[
KeyFromValue = boost :: multi_index :: const_mem_fun,
Compare = std :: less,
SuperMeta = boost :: multi_index :: detail :: nth_layer <1,MyClass*,编译boost :: multi_index :: detail :: ordered_index' , boost :: multi_index :: indexed_by>,boost :: multi_index :: ordered_unique >>,std :: allocator>,
TagList = boost :: mpl :: vector0,
Category = boost :: multi_index :: detail :: ordered_unique_tag
]
c: \项目\ bad_angle_studios\brak3 \干线\源\源\server\MyClass.cpp(18):请参阅
使用
[
Value = MyClass*,
IndexSpecifierList = boost :: multi_index :: indexed_by>,boost :: multi_index 编译的类模板实例化'boost :: multi_index :: multi_index_container'的引用: :ordered_unique >>
]
c:\ lib\c ++\boost\boost\aligned_storage.hpp(53):错误C2872:'detail':模糊符号
可以是'boost :: detail'
或'boost :: multi_index :: detail'
c:\ lib\c ++\boost\boost\aligned_storage.hpp(56):请参阅
使用
[
size_ = 4,
alignment_ = 4 编译的类模板实例化'boost :: detail :: aligned_storage :: aligned_storage_imp :: data_t'的引用
]
c:\ lib\c ++\boost\boost\aligned_storage.hpp(69):参见
使用
[
size_ = 4,
alignment_ = 4
]
c 编译的类模板实例化'boost :: detail :: aligned_storage :: aligned_storage_imp' :\ lib\c ++\boost\boost\aligned_storage.hpp(73):错误C2872:'detail':模糊符号
可能是'boost :: detail'
或'boost :: multi_index ::
detail'c:\ projects\bad_angle_studios\brak3 \树干\源\源\服务器\ MyClass.cpp(44):错误C2676:二进制'[':'MyClassList'没有定义此运算符或转换为预定义运算符可接受的类型

Ben*_*oît 5

boost :: multi_index是你问题的答案.有关如何使用它的更多信息,请参阅那里.