|
Boost : |
Subject: [boost] Feed pointers to a container, however use the member of the data type which pointer refer to as an index
From: è·¯ é (denglu36_at_[hidden])
Date: 2009-09-21 22:33:52
Dear All:
In the following codes, I feed the container with B*, however I user the member of B as the index. Can somebody explain to me how this work out? And It seems that if smart pointers are feeded into the container, it still works out.
#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <iostream>
using namespace boost::multi_index;
class B
{
public:
   int idx;
};
typedef boost::multi_index_container<
  Â
B*,
   indexed_by<
      ordered_unique<
         BOOST_MULTI_INDEX_MEMBER(B, int, idx)
      >
   >
>Container;
int main()
{
   Container container;
   B* p1 = new B;
   B* p2 = new B;
   p1->idx = 2;
   p2->idx = 1;
   container.insert(p1);
   container.insert(p2);
   Container::iterator iter;
   for(iter=container.begin();iter!=container.end();iter++)
   {
      B* tmp = (*iter);
      std::cout << tmp->idx << std::endl;
   }
   return
0;
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk