如何使用composite_key为multi_index_containder编写自定义谓词?

发布于 2024-09-05 06:33:27 字数 1314 浏览 8 评论 0原文

我用谷歌搜索并搜索了boost的man,但没有找到任何例子。无论如何,这可能是一个愚蠢的问题。

所以我们有这个人的著名电话簿:

typedef multi_index_container<
  phonebook_entry,
  indexed_by<
    ordered_non_unique<
      composite_key<
        phonebook_entry,
        member<phonebook_entry,std::string,&phonebook_entry::family_name>,
        member<phonebook_entry,std::string,&phonebook_entry::given_name>
      >,
      composite_key_compare<
        std::less<std::string>,   // family names sorted as by default
        std::greater<std::string> // given names reversed
      >
    >,
    ordered_unique<
      member<phonebook_entry,std::string,&phonebook_entry::phone_number>
    >
  >
> phonebook;


phonebook pb;
...
// look for all Whites
std::pair<phonebook::iterator,phonebook::iterator> p=
  pb.equal_range(boost::make_tuple("White"), my_custom_comp());

my_custom_comp() 应该是什么样子?我的意思是,对我来说很清楚,它需要 boost::multi_index::composite_key_result 作为参数(由于编译错误:)),但是在这种特殊情况下 CompositeKey 是什么?

struct my_custom_comp
{
    bool operator()( ?? boost::multi_index::composite_key_result<CompositeKey> ?? ) const
    {
        return blah_blah_blah;
    }
};

提前致谢。

I googled and searched in the boost's man, but didn't find any examples. May be it's a stupid question...anyway.

So we have the famous phonebook from the man:

typedef multi_index_container<
  phonebook_entry,
  indexed_by<
    ordered_non_unique<
      composite_key<
        phonebook_entry,
        member<phonebook_entry,std::string,&phonebook_entry::family_name>,
        member<phonebook_entry,std::string,&phonebook_entry::given_name>
      >,
      composite_key_compare<
        std::less<std::string>,   // family names sorted as by default
        std::greater<std::string> // given names reversed
      >
    >,
    ordered_unique<
      member<phonebook_entry,std::string,&phonebook_entry::phone_number>
    >
  >
> phonebook;


phonebook pb;
...
// look for all Whites
std::pair<phonebook::iterator,phonebook::iterator> p=
  pb.equal_range(boost::make_tuple("White"), my_custom_comp());

How should my_custom_comp() look like? I mean it's clear for me then it takes boost::multi_index::composite_key_result<CompositeKey> as an argumen (due to compilation errors :) ), but what is CompositeKey in that particular case?

struct my_custom_comp
{
    bool operator()( ?? boost::multi_index::composite_key_result<CompositeKey> ?? ) const
    {
        return blah_blah_blah;
    }
};

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2024-09-12 06:33:27

它应该看起来像 composite_key_compare。对于您的情况(非模板版本):

typedef composite_key<
    phonebook_entry,
    member<phonebook_entry,std::string,&phonebook_entry::family_name>,
    member<phonebook_entry,std::string,&phonebook_entry::given_name>
  > my_comp_type_t;

struct my_custom_comp
{
    bool operator()( 
        const boost::tuple<const char*>& x,
        const boost::multi_index::composite_key_result<my_comp_type_t>& y ) const
    {
        return false; // should return something instead of false
    }
    bool operator()( 
        const boost::multi_index::composite_key_result<my_comp_type_t>& y,
        const boost::tuple<const char*>& x ) const
    {
        return false; // should return something instead of false
    }
};

It should look like composite_key_compare. For your case (non-template version):

typedef composite_key<
    phonebook_entry,
    member<phonebook_entry,std::string,&phonebook_entry::family_name>,
    member<phonebook_entry,std::string,&phonebook_entry::given_name>
  > my_comp_type_t;

struct my_custom_comp
{
    bool operator()( 
        const boost::tuple<const char*>& x,
        const boost::multi_index::composite_key_result<my_comp_type_t>& y ) const
    {
        return false; // should return something instead of false
    }
    bool operator()( 
        const boost::multi_index::composite_key_result<my_comp_type_t>& y,
        const boost::tuple<const char*>& x ) const
    {
        return false; // should return something instead of false
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文