2009-12-25 76 views
2

我有一大堆对象(可能是1000年),我需要存储在一个容器中。我需要能够通过两种方式找到特定实例,无论是通过其ID号(64位无符号整数)还是其名称(std :: string)。一般来说,ID将是最常见的,但在某些情况下,名称是已知的,但不是ID。有多个键类型的关联数组,可能吗?

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

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

好吧,我想我会给予多指数一试,因为我有提振反正,但我似乎无法得到它来编译即使我做了完全一样的文档中,据我可以告诉:(

测试代码:

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; 

和一般的升压模板错误...

c:\lib\c++\boost\boost\aligned_storage.hpp(69) : error C2872: 'detail' : ambiguous symbol
could be 'boost::detail'
or 'boost::multi_index::detail'
c:\lib\c++\boost\boost\multi_index\detail\index_node_base.hpp(42) : see reference to class template instantiation 'boost::aligned_storage' being compiled
with
[
size_=4,
alignment_=4
]
c:\lib\c++\boost\boost\multi_index\detail\index_node_base.hpp(47) : see reference to class template instantiation 'boost::multi_index::detail::pod_value_holder' being compiled
with
[
Value=MyClass *
]
c:\lib\c++\boost\boost\multi_index\detail\ord_index_node.hpp(582) : see reference to class template instantiation 'boost::multi_index::detail::index_node_base' being compiled
with
[
Value=MyClass *,
Allocator=std::allocator
]
c:\lib\c++\boost\boost\multi_index\ordered_index.hpp(137) : see reference to class template instantiation 'boost::multi_index::detail::ordered_index_node' being compiled
with
[
Super=boost::multi_index::detail::index_node_base>
]
c:\lib\c++\boost\boost\multi_index\ordered_index.hpp(119) : see reference to class template instantiation 'boost::multi_index::detail::ordered_index' being compiled
with
[
KeyFromValue=boost::multi_index::const_mem_fun,
Compare=std::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) : see reference to class template instantiation 'boost::multi_index::detail::ordered_index' being compiled
with
[
KeyFromValue=boost::multi_index::const_mem_fun,
Compare=std::less,
SuperMeta=boost::multi_index::detail::nth_layer<1,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:\projects\bad_angle_studios\brak3\trunk\source\source\server\MyClass.cpp(18) : see reference to class template instantiation 'boost::multi_index::multi_index_container' being compiled
with
[
Value=MyClass *,
IndexSpecifierList=boost::multi_index::indexed_by>,boost::multi_index::ordered_unique>>
]
c:\lib\c++\boost\boost\aligned_storage.hpp(53) : error C2872: 'detail' : ambiguous symbol
could be 'boost::detail'
or 'boost::multi_index::detail'
c:\lib\c++\boost\boost\aligned_storage.hpp(56) : see reference to class template instantiation 'boost::detail::aligned_storage::aligned_storage_imp::data_t' being compiled
with
[
size_=4,
alignment_=4
]
c:\lib\c++\boost\boost\aligned_storage.hpp(69) : see reference to class template instantiation 'boost::detail::aligned_storage::aligned_storage_imp' being compiled
with
[
size_=4,
alignment_=4
]
c:\lib\c++\boost\boost\aligned_storage.hpp(73) : error C2872: 'detail' : ambiguous symbol
could be 'boost::detail'
or 'boost::multi_index::detail'
c:\projects\bad_angle_studios\brak3\trunk\source\source\server\MyClass.cpp(44) : error C2676: binary '[' : 'MyClassList' does not define this operator or a conversion to a type acceptable to the predefined operator

+0

对不起,我的笔记本电脑上没有开发环境。如果在typedef'ing MyClassList时使用common :: MyBaseClass而不是MyClass,它会编译吗? – 2009-12-25 15:50:33

+0

好吧,我把它改为基类,并使用“cosnt std :: string&”,但是现在我得到了一个不同的错误... – 2009-12-25 18:52:45

+0

好吧我修改了主帖以反映他的变化和新错误 – 2009-12-25 18:56:00

回答

4

boost :: multi_index是你的问题的答案。有关如何使用它的更多信息,请参见there

0

两张地图(其中一张以ID为关键字,另一张以名称为关键字)的方法对我来说似乎很好。这很容易实现,并且会很好地工作。

我看到其他答案推荐boost库。如果您已经在项目中使用boost,那么它可能是一个很好的解决方案。如果你不这样做 - 我不确定为这个简单的案例增加你的项目是否值得。

+0

只是你知道:当你只需要有限数量的boost库时,你可以使用一个简单的工具bcp来提取它们。这样,即使在提升之后,你也不会为你不需要的东西付费。 – 2009-12-25 15:40:34

-1

您可以将数据存储在std :: vector中,并使用std :: find算法来查找您的项目。 find算法接受不同的比较器,所以只需定义一个匹配id和另一个匹配名称的匹配器。

这一发现算法比的std ::地图和std ::较慢集的查找方法,所以如果性能是一个大问题,那么你很可能对速度更好的交易空间,或者使用2个地图或使用升压

编辑,只是有一个想法。将数据存储在地图中,使用id作为关键字,因为这是常见的情况。然后使用std :: find算法和一个与名称匹配的非常见情况的谓词。这应该减少(但不是删除)的性能问题

+0

“慢”是轻描淡写,std :: find是一个简单的O(n)向前搜索。如果你关心优化查找时间,你不应该使用它。 – 2009-12-26 06:21:29

+0

@特里,是的,我知道。然而,OP没有提到性能要求,所以我指出了他们的可能性 – Glen 2009-12-26 09:57:30

1

这是上述的另一种替代方案,您选择哪种解决方案取决于您的需求。抓取SqlLite将关于您的对象的数据存储在数据库中并为其运行查询。

1

Fire Lancer,你不是正确的鉴定Boost.MultiIndex的名字,而是例如boost::indexed_by你必须写boost::multi_index::indexed_by等等。