如何使用boost::multi_index复合密钥
How to use boost::multi_index composite keys
我的需求需要这样的映射:
pair<string key1, string key2> _keypair;
map<_keypair, shared_ptr<classX>>
我的需求是:
key1和key2对必须是唯一的。
我应该能够使用key1和Key2对进行访问。
插入。
使用复合密钥删除
我遇到了boost::multi_index
,但不太清楚。有人能给我举个例子说明我的情况吗?
smth是这样的(我没有编译这个代码):
struct value_type
{
std::string key1_;
std::string key2_;
std::shared_ptr<some_class> ptr_;
};
// index tags (otherwise you have to use number when get<N>() corresponding index)
struct composite_index;
struct key1_index;
struct key2_index;
using namespace boost::multi_index;
typedef multi_index_container<
value_type
, indexed_by<
ordered_unique<
tag<composite_index>
, composite_key<
value_type
, member<value_type, std::string, &value_type::key1_>
, member<value_type, std::string, &value_type::key2_>
>
>
, ordered_non_unique<
tag<key1_index>
, member<value_type, std::string, &value_type::key1_>
>
>
, ordered_non_unique<
tag<key2_index>
, member<value_type, std::string, &value_type::key2_>
>
>
> index_type;
index_type a;
a.insert({"key-1", "key-2", std::shared_ptr<some_class>(new some_class(...)});
// find by key1_index (or key2_index)
auto range = a.get<key1_index>().equal_range("key-1");
// find by composite index
auto it = a.get<composite_index>().find(std::make_tuple("key-1", "key-2"));
// erase by any iterator type
a.erase(it);
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- DrawIndexedInstanced 具有不同的 Index Count per Instance (Directx
- 算术复合运算符重载为非成员
- C++模板函数中的初始化 - 新的初始值设定项表达式列表被视为复合表达式
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- OpenGL VBO Indexing ( How to compute Index Array)
- HDF5Cpp 扩展复合数据集超板问题
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何根据排序索引的向量对 std::index 集进行排序?
- 返回复合模式的复合对象不返回整个对象
- 如何处理模板类中的复合类型
- 重载 C++ 中的复合运算符 (.*)
- C++析构函数调用两次,堆栈分配的复合对象
- H5Tget_member_type() 返回复合 HDF5 数据类型的奇怪值
- 在一行代码中理解 index++
- 复合类型的动态数组
- 为什么这个复合语句作为用大括号和括号括起来的语句序列似乎不是有效的语句表达式
- 对复合分配中的"常量"的混淆
- C 复合赋值运算符 ^= 平均值