gpt4 book ai didi

c++ - boost multi_index_container composite_key_compare

转载 作者:太空宇宙 更新时间:2023-11-04 14:04:34 27 4
gpt4 key购买 nike

我正在尝试编写计算密集型程序。我需要 char* 作为 multi_index_container 的 composite_key_compare 的比较字段。但是,它似乎不起作用。代码如下:

struct MyStruct
{
char* firstName;
char* secondName;
int age;
};

struct equal_char
{ // functor for operator<=
inline bool operator()(const char* left, const char* right) const
{ // apply operator<= to operands
bool result=(strcmp(left,right)==0);
return result;
}
};

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,
composite_key_compare
<equal_char, equal_char>
>
>
> MyContainer;

boost::ptr_vector<MyStruct> vec;
MyStruct* struct1=new MyStruct();
struct1->firstName="Michael";
struct1->secondName="Mike";
struct1->age=20;
vec.push_back(struct1);



MyContainer myContainer;
myContainer.insert(struct1);
char* first="Michael";
char* second="Mike";
auto it=myContainer.find(boost::make_tuple(first, second));
if(it!=myContainer.end())
cout << (*it)->age << endl;

我确实跟踪了 equal_char,发现它确实在“Michael”与“Michael”的第一次比较中返回了 true,但我还发现在“Mike”与“”的第二次比较中没有调用 equal_char麦克风”。谁能帮我解决这个问题? composite_key_compare应该怎么写?

最佳答案

我跟踪了这​​个程序,发现 boost 需要像 std::less() 这样的东西来进行“ordered unique”的比较操作。

所以,我添加了下面的代码,它起作用了。

struct CompareLess
{ // functor for operator<=
#pragma region For 1 variable
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;
}

#pragma endregion For 1 variable
#pragma region For 2 variables
static inline int compare(const boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*, char*>& y)
{
int val=strcmp(k.value->firstName, y.get<0>());
if(val!=0)
return val;
else
return compare(y.get<1>(),(const char*)(k.value->secondName));
}
inline bool operator()(const boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*, char*>& y) const
{
return compare(k,y) <0;
}

static inline int compare(const boost::tuple<char*, char*>& y, const boost::multi_index::composite_key_result<comp_key>& k)
{
return -compare(k,y);
}
inline bool operator()(const boost::tuple<char*, char*>& y, const boost::multi_index::composite_key_result<comp_key>& k) const
{
return compare(y,k)<0;
}
#pragma endregion For 2 variables
#pragma region For all variables
inline bool operator()(const boost::multi_index::composite_key_result<comp_key>& k1,
const boost::multi_index::composite_key_result<comp_key>& k2) const
{
return CompareLess()((const char*)(k1.value->firstName), (const char*)(k2.value->firstName));
}
#pragma endregion For all variables



};

typedef multi_index_container
<
MyStruct*,
indexed_by
<
ordered_unique
<
comp_key,
CompareLess
>
>
> MyContainer;

但是我有更多的问题,这是非常冗长的代码,并且假设如果我有更多的字段,它将会更长得多。有什么聪明的方法可以做到这一点吗?

关于c++ - boost multi_index_container composite_key_compare,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17626089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com