对于下面的代码:
struct MyStruct
{
char* firstName;
char* secondName;
int age;
};
typedef composite_key
<MyStruct*,
BOOST_MULTI_INDEX_MEMBER(MyStruct, char*, firstName),
BOOST_MULTI_INDEX_MEMBER(MyStruct, char*, secondName)
> comp_key;
typedef multi_index_container
<
MyStruct*,
indexed_by
<
ordered_unique
<
comp_key,
CompareLess
>
>
> MyContainer;我可以很容易地写出一个无关紧要的代码,如下所示:
struct CompareLess
{ // functor for operator<
static inline int compare(const char* left, const char* right)
{
return strcmp(left, right);
}
inline bool operator()(const char* left, const char* right) const
{ // apply operator<= to operands
return compare(left, right)<0;
}
static inline int compare(const boost::tuple<char*>& x, const char*y)
{
return compare(x.get<0>(),y);
}
inline bool operator()(const boost::tuple<char*>& x, const char*y) const
{
return compare(x,y)<0;
}
static inline int compare(const boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*>& y)
{
return -compare(y,(const char*)(k.value->firstName));
}
inline bool operator()(const boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*>& y) const
{
return compare(k,y)<0;
}
static inline int compare(const boost::tuple<char*>& y, const boost::multi_index::composite_key_result<comp_key>& k)
{
return compare(y,(const char*)(k.value->firstName));
}
inline bool operator()(const boost::tuple<char*>& y, const boost::multi_index::composite_key_result<comp_key>& k) const
{
return compare(y,k) <0;
}
}但是当我想写下面这样的代码时:
typedef composite_key
<double,
char*,
char*
> comp_key;我在使用下面的函数编写compareLess时遇到了问题
static inline int compare(const boost::tuple<char*>& y, const boost::multi_index::composite_key_result<comp_key>& k)
{
return compare(y,(const char*)(k.value->firstName));
}我不知道如何编写一些代码"k.value->firstName“来获取char*进行比较,因为值不再是一个结构,它只是一个双精度值。那么,我可以从哪里获得用于比较的字段呢?有没有像k.get<0>()这样的东西?
发布于 2013-07-16 22:43:27
组合键的比较条件通过composite_key_compare指定。在您的特定情况下,您可能需要如下内容
ordered_unique<
comp_key,
composite_key_compare<
std::less<double>,
CompareLess,
Compareless
>
>其中,只需使用const char*s即可调用CompareLess:
struct CompareLess
{
static inline int compare(const char* left, const char* right)
{
return strcmp(left, right);
}
inline bool operator()(const char* left, const char* right) const
{
return compare(left, right)<0;
}
};脚手架的其余部分由composite_key_compare提供。
https://stackoverflow.com/questions/17667001
复制相似问题