Boost logo

Boost Users :

From: Joaquín Mª López Muñoz (joaquin_at_[hidden])
Date: 2007-02-23 04:24:35


Boris ha escrito:

> struct employee {
> std::string name;
> };
>
> typedef multi_index_container<
> employee,
> indexed_by<
> hashed_unique<member<employee, std::string, &employee::name> >
> >
> > mi;
>
> If I want to change the index to make it case-insensitive I have to write
> my own key extractor, right? Would the following code then be correct (it
> is based on boost::multi_index::member)?

[...]

Yep, it is correct, though possibly a little overkill; you could use a simpler
key
extractor for the task at hand, like this:

struct employee_case_insensitive_name_extractor
{
   typedef std::string result_type;

   std::string operator()(const employee& x) const
   {
     return boost::algorithm::to_lower_copy(x.name);
   }
};

> With the following typedef I get a container which does not change the
> name of an employee (I want to retain the name without making it lower- or
> uppercase) but refuses items with the same case-insensitive name?
>
> typedef multi_index_container<
> employee,
> indexed_by<
> hashed_unique<caseinsensitive_string<employee, &employee::name> >
> >
> > mi2;

Well, you just have to try in order to find out :) Looks pretty much like
it'll
do what you want.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net