Boost logo

Boost Users :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2007-06-27 11:39:04


----- Mensaje original -----
De: hongleij_at_[hidden]
Fecha: Miércoles, Junio 27, 2007 10:16 am
Asunto: [Boost-users] [multi-index] how to use in herit member as
index
Para: boost-users_at_[hidden]

> //boost
> #include <boost/multi_index_container.hpp>
> #include <boost/multi_index/hashed_index.hpp>
> #include <boost/multi_index/member.hpp>
> using namespace ::boost;
> using namespace ::boost::multi_index;
>
> #include <string>
>
> struct Peer
> {
> std::string userid;
> };
> struct CPeer:public Peer
> {
> int index;
> };
> struct userid{};
>
> typedef multi_index_container<
> CPeer,
> indexed_by<
>
> hashed_unique<tag<userid>,member<CPeer,std::string,&CPeer::userid> >
> >
> > CPeersType;
>
> //how could i use userid as an index in CPeer ,not in Peer,

Hello,

If you're using Boost 1.34, then you can simply use

  hashed_unique<
      tag<userid>,
      member<Peer,std::string,&Peer::userid> >

(note we're writing Peer here rather than CPeer.) If you can't
afford upgrading to Boost 1.34, you must use a custom key
extractor:

  struct UseridExtractor
  {
      typedef std::string result_type;
  
      const std::string& operator()(const CPeer& cp)const
      {
          return cp.userid;
      }
  };

  typedef multi_index_container<
     CPeer,
     indexed_by<
        hashed_unique<tag<userid>,UseridExtractor>
>
> CPeersType;

(more on custom key extractors at http://tinyurl.com/39shzv ).
Thank you for using Boost.MultiIndex,

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