multi-index container random access index ordered
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. e. for (size_t i = 0; i < a_arr.size(); i++) { std::cout << a_arr[i].first << std::endl; } will return 1 3 2 5 4 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 suspect I could do like this: a_arr.insert(a_set.find(1), std::make_pair(1, 0)); but it errors about incompatible types of iterators for what a_set.find returns and what a_arr.insert accepts. Or… may be there are some kind of definition of random access index with respect to another index which is ordered??? I don’t know. Your help is appreciated.
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
participants (2)
-
Joaquin M Lopez Munoz
-
Vladimir Voronkov