Boost logo

Boost Users :

Subject: Re: [Boost-users] multi-index container random access index ordered
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2009-11-20 12:00:56


Vladimir Voronkov <voronkovv <at> mail.ru> writes:

>
> Hi,
>
> I defined a container like this:
>
> typedef boost::multi_index_container<
> std::pair<int, int>,
> boost::multi_index::indexed_by<
> boost::multi_index::random_access<>,
> boost::multi_index::ordered_unique<
> boost::multi_index::member<
> std::pair<int, int>, int, &std::pair<int, int>::first>
> > > > IdContainer;
>
> typedef IdContainer::nth_index<0>::type IdsArr;
> typedef IdContainer::nth_index<1>::type IdsSet;
>
> and then I insert a data into it:
>      
> a_set.insert(std::make_pair(1, 0));
> a_set.insert(std::make_pair(3, 0));
> a_set.insert(std::make_pair(2, 0));
> a_set.insert(std::make_pair(5, 0));
> a_set.insert(std::make_pair(4, 0));
>
> in this case elements will be “pushed back” in terms
> of random access index [...]
> I would like to have them inserted ordered way, and
> as long as we have ordered index I suppose it’s possible.
> How can I do that?

I can think of two ways:

a)

  void insert_in_place(IdContainer& a,const std::pair<int,int>& x)
  {
    a.insert(a.project<0>(a.get<1>().lower_bound(x.first)),x);
  }
  ...
  insert_in_place(a, std::make_pair(1, 0));
  insert_in_place(a, std::make_pair(3, 0));
  insert_in_place(a, std::make_pair(2, 0));
  insert_in_place(a, std::make_pair(5, 0));
  insert_in_place(a, std::make_pair(4, 0));

b)

  a_set.insert(std::make_pair(1, 0));
  a_set.insert(std::make_pair(3, 0));
  a_set.insert(std::make_pair(2, 0));
  a_set.insert(std::make_pair(5, 0));
  a_set.insert(std::make_pair(4, 0));

  a_arr.rearrange(a_set.begin());

a) makes each insertion in place, while b) rearranges
the random access index after the inserions are completed.
Boost.MultiIndex docs provide info on both projection
operations and rearrange operations for you to consult.

HTH,

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