$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [Boost-users] multi index container
From: joaquin_at_[hidden]
Date: 2010-02-03 09:46:13
Dejan escribió:
> Hi all,
>
> I have to store pointer of type (let's say) "employee" in a
> multi-index-container:
>
> struct employee
> {
>   employee(int id):id(id){}
>   int         id;
> };
>
> Further I have to check if every id is unique:
>
> employee* e0 = new employee(1);
> employee* e1 = e0;
> employee* e2 = new employee(1);
> employee* e3 = new employee(2);
>
> my_multi_index_conatiner.insert(e0); // OK
> my_multi_index_conatiner.insert(e1); // BAD
> my_multi_index_conatiner.insert(e2); // BAD
> my_multi_index_conatiner.insert(e0); // OK
>   
(I guess you meant e3 in the line above.)
> Can someone tell me how a typedef of a multi_index_container fullfilling
> prerequisities above looks? 
The following would do:
  typedef multi_index_container<
    employee*,
    indexed_by<
      ordered_unique<
        member<employee,int,&employee::id>
      >
    >
  > multi_t;
The lib is smart enough that can index by employee::id even if what you 
store are
not employees proper, but pointers to employee.
> Or is a multi_index_container not the right
> tool?
>   
Using Boost.MultiIndex might be overkill here if this is all you need 
from it:
an std::set can be used also with a suitable compare predicate.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo