views:

79

answers:

2

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.

+1  A: 

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
    }
};
Kirill V. Lyadvinsky
Could you please point out what is CompositeKey type (which used here - composite_key_result<CompositeKey>) in the example above?I do appreciate the answer.
Titan
`CompositeKey` is a template argument. Check link in my answer.
Kirill V. Lyadvinsky
Thank you very very much :)
Titan
@Kirill,What is going on up there? Could you possibly post a full example? I am not smart enough I'm afraid to understand the link you provided.
shaz
@shaz, full example will be too massive, try to start from [Boost MultiIndex documentation](http://www.boost.org/doc/libs/1_43_0/libs/multi_index/doc/tutorial/index.html) and so then you could ask more specific questions.
Kirill V. Lyadvinsky
A: 
composite_key_compare<
    std::less<std::string>,   // family names sorted as by default
    std::greater<std::string> // given names reversed
  >

bool b;

if (b == true)

// given names sorted as by default

else if (b == false)

// given names reversed

How define the multi_index_container???