boost多个关键字索引multi_index_container

根据不同的索引排序结构体。其中tag的意思是指定一个标记,如果不指定的话默认是从0开始,以下例子展示了这两种情况

代码:

  1. #include <string>  
  2. #include <iostream>  
  3. #include <boost/multi_index_container.hpp>  
  4. #include <boost/multi_index/member.hpp>  
  5. #include <boost/multi_index/ordered_index.hpp>  
  6.   
  7. using namespace boost;  
  8. using namespace boost::multi_index;  
  9. using namespace std;  
  10. struct Person{  
  11.   int id;  
  12.   int age;  
  13.   int height;  
  14.   string name;  
  15.   
  16.   Person(int id_,int age_,int height_,std::string name_):  
  17.     id(id_),  
  18.     age(age_),  
  19.     height(height_),  
  20.     name(name_)  
  21.     {}  
  22. };  
  23.   
  24. std::ostream& operator<<(std::ostream& os,const Person& ps)  
  25. {  
  26.   os<<ps.id<<" "<<ps.age<<" "<<ps.height<<" "<<ps.name<<" "<<std::endl;  
  27.   return os;  
  28. }  
  29.   
  30. typedef multi_index_container<  
  31.   Person,  
  32.   indexed_by<  
  33.     ordered_unique<member<Person, int, &Person::id> >,  
  34.     ordered_non_unique<member<Person, int, &Person::age> >,  
  35.     ordered_non_unique<member<Person, int, &Person::height> >,  
  36.     ordered_non_unique<member<Person, string, &Person::name> >   
  37. > >PersonContainer;  
  38.   
  39. typedef PersonContainer::nth_index<0>::type IdIndex;  
  40. typedef PersonContainer::nth_index<1>::type AgeIndex;  
  41. typedef PersonContainer::nth_index<2>::type HeightIndex;  
  42. typedef PersonContainer::nth_index<3>::type NameIndex;  
  43.   
  44.   
  45.   
  46.   
  47. struct person_id{};  
  48. struct person_age{};  
  49. struct person_height{};  
  50. struct person_name{};  
  51.   
  52. typedef multi_index_container<  
  53.   Person,indexed_by<  
  54.     ordered_unique< tag<person_id>,member<Person, int, &Person::id> >,  
  55.     ordered_non_unique< tag<person_age>,member<Person, int, &Person::age> >,  
  56.     ordered_non_unique< tag<person_height>,member<Person, int, &Person::height> >,  
  57.     ordered_non_unique<tag<person_name>,member<Person, string, &Person::name> >  
  58. > >PersonContainerTag;   
  59.   
  60. int main(){  
  61.   PersonContainer con;  
  62.   con.insert(Person(2,31,170,"aliu"));  
  63.   con.insert(Person(1,27,164,"dliu"));  
  64.   con.insert(Person(0,55,182,"cliu"));  
  65.   con.insert(Person(3,51,142,"bliu"));  
  66.   
  67.   
  68.   IdIndex& ids = con.get<0>();  
  69.   copy(ids.begin(),ids.end(), ostream_iterator<Person>(cout));  
  70.   cout << endl;  
  71.   
  72.   AgeIndex& ages = con.get<1>();  
  73.   copy(ages.begin(), ages.end(), ostream_iterator<Person>(cout));  
  74.   cout << endl;  
  75.   
  76.   HeightIndex& heights = con.get<2>();  
  77.   copy(heights.begin(), heights.end(), ostream_iterator<Person>(cout));  
  78.   cout << endl;  
  79.   
  80.   NameIndex& names = con.get<3>();  
  81.   copy(names.begin(), names.end(), ostream_iterator<Person>(cout));  
  82.   cout << endl;  
  83.   
  84.   
  85.   
  86.   PersonContainerTag con_tag;  
  87.   con_tag.insert(Person(2,31,170,"aliu"));  
  88.   con_tag.insert(Person(1,27,164,"dliu"));  
  89.   con_tag.insert(Person(0,55,182,"cliu"));  
  90.   con_tag.insert(Person(3,51,142,"bliu"));  
  91.   
  92.   auto& ids_tag = con_tag.get<person_id>();  
  93.   copy(ids_tag.begin(),ids_tag.end(), ostream_iterator<Person>(cout));  
  94.   cout << endl;  
  95.   
  96.   auto& ages_tag = con_tag.get<person_age>();  
  97.   copy(ages_tag.begin(), ages_tag.end(), ostream_iterator<Person>(cout));  
  98.   cout << endl;  
  99.   
  100.   auto& heights_tag = con_tag.get<person_height>();  
  101.   copy(heights_tag.begin(), heights_tag.end(), ostream_iterator<Person>(cout));  
  102.   cout << endl;  
  103.   
  104.   auto& names_tag = con_tag.get<person_name>();  
  105.   copy(names_tag.begin(), names_tag.end(), ostream_iterator<Person>(cout));  
  106.   cout << endl;  
  107.   
  108.   return 0;  
  109. }  

结果:
  1. 0 55 182 cliu   
  2. 1 27 164 dliu   
  3. 2 31 170 aliu   
  4. 3 51 142 bliu   
  5.   
  6. 1 27 164 dliu   
  7. 2 31 170 aliu   
  8. 3 51 142 bliu   
  9. 0 55 182 cliu   
  10.   
  11. 3 51 142 bliu   
  12. 1 27 164 dliu   
  13. 2 31 170 aliu   
  14. 0 55 182 cliu   
  15.   
  16. 2 31 170 aliu   
  17. 3 51 142 bliu   
  18. 0 55 182 cliu   
  19. 1 27 164 dliu   
  20.   
  21. 0 55 182 cliu   
  22. 1 27 164 dliu   
  23. 2 31 170 aliu   
  24. 3 51 142 bliu   
  25.   
  26. 1 27 164 dliu   
  27. 2 31 170 aliu   
  28. 3 51 142 bliu   
  29. 0 55 182 cliu   
  30.   
  31. 3 51 142 bliu   
  32. 1 27 164 dliu   
  33. 2 31 170 aliu   
  34. 0 55 182 cliu   
  35.   
  36. 2 31 170 aliu   
  37. 3 51 142 bliu   
  38. 0 55 182 cliu   
  39. 1 27 164 dliu  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值