读书人

boost里面multi index container使用求

发布时间: 2013-10-29 12:07:57 作者: rapoo

boost里面multi index container使用求教。谢谢
我有个结构体,里面有字符串和其他字段,我想用字符串和另外几个字段做主键。怎么定义这个multi_index_container
例如:
结构体
struct TOrder
{
char szDate[9]; //日期
int nID; //订单ID
int nProductID; //产品ID
int nVol; //购买数量
……
};
需要用日期和订单ID做主键,该怎么定义composite_key?
谢谢 composite_key multi_index?boost boost multi_index
[解决办法]
http://www.boost.org/doc/libs/1_54_0/libs/multi_index/doc/tutorial/indices.html

参考这段,订单ID可以直接ordered_unique限定(ID应该是唯一的吧)
日期数组最好包装一下比较方法

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>

struct employee
{
int id;
std::string name;
int ssnumber;

employee(int id,const std::string& name,int ssnumber):
id(id),name(name),ssnumber(ssnumber){}

bool operator<(const employee& e)const{return id<e.id;}
};

typedef multi_index_container<
employee,
indexed_by<
// sort by employee::operator<
ordered_unique<identity<employee> >,

// sort by less<string> on name
ordered_non_unique<member<employee,std::string,&employee::name> >,

// hashed on ssnumber
hashed_unique<member<employee,int,&employee::ssnumber> >
>
> employee_set

[解决办法]
联合主键吗?
你是想确保业务唯一性还是主键唯一?
如果是后者,uuid。

读书人网 >C++

热点推荐