2014-07-22 79 views
0

我试图将对象存储在boost multi-index容器中。多索引中的多个唯一键

这些对象都是唯一的,可以通过2个独立的键(也是唯一的)进行检索。

namepsace bm = boost::multi_index; 

class MyObj { 
string strid_; 
int32_t numid_; 
}; 

//! associative container searchable by ClOrdId and Sunofia Id. 
typedef boost::multi_index_container< MyObj, 
bm::indexed_by< 
    bm::ordered_unique< 
    bm::member<MyObj,string,&MyObj::strid_> 
    >, 
    bm::ordered_unique< 
    bm::member<MyObj,int32,&MyObj::numid_> 
    > 
>    
> Cntr; 
Cntr cntr_; 

当我试图找到整数我用下面的代码

int32_t to_find = 12; 
Cntr::iterator it = cntr_.find(id); 

但它不会编译指数的任何元素,我得到以下错误

error: invalid conversion from ‘int’ to ‘const char*’ 

当我用字符串使用相同的代码时,它工作正常;你知道我做错了什么吗?

回答

2
auto it = cntr_.get<1>().find(id); 

每个索引被单独访问(通过get),并且具有其自身的成员函数,迭代器等(如果你不能使用autoitCntr::nth_index<1>::type::iterator型的。)更多信息放在doc tutorial

+0

嗨!它工作正常!我非常感谢你! –

+0

嗨!只是注意到你是创建库的人!!!啊!好的东西的确如此,大量的代码行少写! :) –

+0

很高兴知道它是有用的:-) –