printing data sorted based on a specific key in boost multi-index container
Hi Joaquin, Supposing I have a container having three unique keys, Is there a way I can print data sorted by one of they keys? At the moment I tried a raw dump - it shows fields based on the first key (which is key1). Thanks Ramesh ---- typedef multi_index_container< MyMap, indexed_by< ordered_unique< tag <Key1>, BOOST_MULTI_INDEX_MEMBER(MyMap,std::string,Key1)>, ordered_non_unique< tag<Key2>,BOOST_MULTI_INDEX_MEMBER(MyMap,unsigned long,Key2)>, ordered_unique< tag<Key3>,BOOST_MULTI_INDEX_MEMBER(MyMap,unsigned long,Key3)> >
tMyMap;
class MyContainer { private: tMyMap MapObj; public: Show(){ for (MyMap::iterator it = MyContainer.begin(), itend = MyContainer.end(); it != itend;++it) { cout << it->Key1; cout << it->Key2; cout << it->Key3; } } }; main() { MyContainer M; // Assuming I have data inserted in the container like // ("Id 4", 10, 10) // ("Id 1", 10, 40) // ("Id 5", 30, 60) M.Show() // The result is as below: //("Id 5", 30, 60) // ("Id 1", 10, 40) // ("Id 4", 10, 10) }
Ramesh escribió:
Hi Joaquin,
Supposing I have a container having three unique keys, Is there a way I can print data sorted by one of they keys? At the moment I tried a raw dump - it shows fields based on the first key (which is key1).
Hi Ramesh, Just use the iterators provided by the second or third index. For instance (not compiled, beware typos): for (MyMap::index_iterator<Key2>::type it = MyContainer.get<Key2>().begin(), itend = MyContainer.get<Key2>().end(); it != itend;++it) { cout << it->Key1; cout << it->Key2; cout << it->Key3; } Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (2)
-
joaquin@tid.es
-
Ramesh