Question

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.

Was it helpful?

Solution

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
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top