我正在尝试将对象存储在 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*’
当我对字符串使用相同的代码时,它工作正常;你知道我做错了什么吗?
最佳答案
auto it = cntr_.get<1>().find(id);
每个索引都是单独访问的(通过 get
)并且有自己的成员函数、迭代器等。(如果您不能使用 auto
, it
是 Cntr::nth_index<1>::type::iterator
类型。)更多信息在文档上 tutorial .
关于algorithm - 多索引中的多个唯一键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24881689/