algorithm - 多索引中的多个唯一键

标签 algorithm stl boost-multi-index

我正在尝试将对象存储在 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 )并且有自己的成员函数、迭代器等。(如果您不能使用 autoitCntr::nth_index<1>::type::iterator 类型。)更多信息在文档上 tutorial .

关于algorithm - 多索引中的多个唯一键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24881689/

相关文章:

c++ - boost multi_index_container 损坏的索引

algorithm - 如何利用 GPS 数据流预测预计到达时间 (ETA)?

algorithm - 绘制图形的遗传算法?职位分配问题

模拟现场投票的算法

c++ - 如何删除 boost :multi_index applying a "deleter" 中的所有元素

boost - 使用复合键从 multi_index_container 中删除每个键的元素

c++ - 循环无限运行C++以解决Knight Tour问题

c++ - 如何将结构指针插入STL vector 并显示内容

c++ - 有哪些 C++ 标准库使用最佳实践?

c++ - STL通过 map C++反向循环