boost::multi_index 多索引容器

#include "stdafx.h"
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/ordered_index.hpp>
using namespace std;
using namespace boost;
/*
定义一个MutiStruct
key  :    1. objectID
          2. strName

value: 对象指针STNodePtr
*/
struct MutiStruct
{
    int      objectID;//唯一    
    string    strName;//唯一    
                    
    STNodePtr ptrNode;//设备对象指针
};

/*
定义容器,索引
*/
using MutiContainer = boost::multi_index::multi_index_container <
    MutiStruct,
    boost::multi_index::indexed_by <
        boost::multi_index::ordered_unique<boost::multi_index::member<MutiStruct, int, &MutiStruct::objectID>>,
        boost::multi_index::ordered_unique<boost::multi_index::member<MutiStruct, string, &MutiStruct::strName>>
    >
>;

/*
有多少个key,就定义多少个枚举,
0:表示以第一个索引作为key。
1:表示以第二个索引作为key。
*/ enum { KEY_1_VIEW = 0, KEY_2_VIEW = 1 }; using MI_OBJ_IDX = MutiContainer::nth_index<KEY_1_VIEW>::type; using MI_NAME_IDX = MutiContainer::nth_index<KEY_2_VIEW>::type;
/*插入数据到容器*/
MutiContainer Conter;

MutiStruct mi;
mi.objectID = 11;
mi.strName = "nihao";

std::pair<MutiContainer::iterator, bool> p = Conter.insert(mi);
if (!p.second)
{
     printf("insert fail 
");
}
原文地址:https://www.cnblogs.com/osbreak/p/9825538.html