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>>
    >
>;

/*
容器分类
*/
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 \n");
}

猜你喜欢

转载自www.cnblogs.com/osbreak/p/9825538.html