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

代码:

[cpp] view plain copy
  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. using namespace boost;
  7. using namespace boost::multi_index;
  8. using namespace std;
  9. struct Person{
  10. int id;
  11. int age;
  12. int height;
  13. string name;
  14. Person(int id_,int age_,int height_,std::string name_):
  15. id(id_),
  16. age(age_),
  17. height(height_),
  18. name(name_)
  19. {}
  20. };
  21. std::ostream& operator<<(std::ostream& os,const Person& ps)
  22. {
  23. os<<ps.id<<" "<<ps.age<<" "<<ps.height<<" "<<ps.name<<" "<<std::endl;
  24. return os;
  25. }
  26. typedef multi_index_container<
  27. Person,
  28. indexed_by<
  29. ordered_unique<member<Person, int, &Person::id> >,
  30. ordered_non_unique<member<Person, int, &Person::age> >,
  31. ordered_non_unique<member<Person, int, &Person::height> >,
  32. ordered_non_unique<member<Person, string, &Person::name> >
  33. > >PersonContainer;
  34. typedef PersonContainer::nth_index<0>::type IdIndex;
  35. typedef PersonContainer::nth_index<1>::type AgeIndex;
  36. typedef PersonContainer::nth_index<2>::type HeightIndex;
  37. typedef PersonContainer::nth_index<3>::type NameIndex;
  38. struct person_id{};
  39. struct person_age{};
  40. struct person_height{};
  41. struct person_name{};
  42. typedef multi_index_container<
  43. Person,indexed_by<
  44. ordered_unique< tag<person_id>,member<Person, int, &Person::id> >,
  45. ordered_non_unique< tag<person_age>,member<Person, int, &Person::age> >,
  46. ordered_non_unique< tag<person_height>,member<Person, int, &Person::height> >,
  47. ordered_non_unique<tag<person_name>,member<Person, string, &Person::name> >
  48. > >PersonContainerTag;
  49. int main(){
  50. PersonContainer con;
  51. con.insert(Person(2,31,170,"aliu"));
  52. con.insert(Person(1,27,164,"dliu"));
  53. con.insert(Person(0,55,182,"cliu"));
  54. con.insert(Person(3,51,142,"bliu"));
  55. IdIndex& ids = con.get<0>();
  56. copy(ids.begin(),ids.end(), ostream_iterator<Person>(cout));
  57. cout << endl;
  58. AgeIndex& ages = con.get<1>();
  59. copy(ages.begin(), ages.end(), ostream_iterator<Person>(cout));
  60. cout << endl;
  61. HeightIndex& heights = con.get<2>();
  62. copy(heights.begin(), heights.end(), ostream_iterator<Person>(cout));
  63. cout << endl;
  64. NameIndex& names = con.get<3>();
  65. copy(names.begin(), names.end(), ostream_iterator<Person>(cout));
  66. cout << endl;
  67. PersonContainerTag con_tag;
  68. con_tag.insert(Person(2,31,170,"aliu"));
  69. con_tag.insert(Person(1,27,164,"dliu"));
  70. con_tag.insert(Person(0,55,182,"cliu"));
  71. con_tag.insert(Person(3,51,142,"bliu"));
  72. auto& ids_tag = con_tag.get<person_id>();
  73. copy(ids_tag.begin(),ids_tag.end(), ostream_iterator<Person>(cout));
  74. cout << endl;
  75. auto& ages_tag = con_tag.get<person_age>();
  76. copy(ages_tag.begin(), ages_tag.end(), ostream_iterator<Person>(cout));
  77. cout << endl;
  78. auto& heights_tag = con_tag.get<person_height>();
  79. copy(heights_tag.begin(), heights_tag.end(), ostream_iterator<Person>(cout));
  80. cout << endl;
  81. auto& names_tag = con_tag.get<person_name>();
  82. copy(names_tag.begin(), names_tag.end(), ostream_iterator<Person>(cout));
  83. cout << endl;
  84. return 0;
  85. }

结果:

[cpp] view plain copy
  1. 0 55 182 cliu
  2. 1 27 164 dliu
  3. 2 31 170 aliu
  4. 3 51 142 bliu
  5. 1 27 164 dliu
  6. 2 31 170 aliu
  7. 3 51 142 bliu
  8. 0 55 182 cliu
  9. 3 51 142 bliu
  10. 1 27 164 dliu
  11. 2 31 170 aliu
  12. 0 55 182 cliu
  13. 2 31 170 aliu
  14. 3 51 142 bliu
  15. 0 55 182 cliu
  16. 1 27 164 dliu
  17. 0 55 182 cliu
  18. 1 27 164 dliu
  19. 2 31 170 aliu
  20. 3 51 142 bliu
  21. 1 27 164 dliu
  22. 2 31 170 aliu
  23. 3 51 142 bliu
  24. 0 55 182 cliu
  25. 3 51 142 bliu
  26. 1 27 164 dliu
  27. 2 31 170 aliu
  28. 0 55 182 cliu
  29. 2 31 170 aliu
  30. 3 51 142 bliu
  31. 0 55 182 cliu
  32. 1 27 164 dliu

boost多个关键字索引multi_index_container相关推荐

  1. Boost.MultiIndex 使用序列索引的示例

    Boost.MultiIndex 使用序列索引的示例 实现功能 C++实现代码 实现功能 Boost.MultiIndex 使用序列索引的示例 C++实现代码 #if !defined(NDEBUG) ...

  2. 使用 [funcref boost::pfr::get] 按索引访问结构体字段的测试程序

    使用 [funcref boost::pfr::get] 按索引访问结构体字段的测试程序 实现功能 C++实现代码 实现功能 使用 [funcref boost::pfr::get] 按索引访问结构体 ...

  3. redis zset转set 反序列化失败_Redis只往zset有序集合添加不存在的数据:关键字索引查询构建+源码分析...

    Redis的有序集合Sorted Set(zset),可以很方便地用来构建关键字索引表,可以很方便地实现支持超大规模并发的关键字组合条件查询. 比如有套博客系统,博客文章存放在 hash 类型 art ...

  4. 一个自动生成关键字索引页面的比处理文件

    电脑上资料多了之后,每次找资料很麻烦.因此,匠人写了这个批处理文件. 1.把这个批处理文件放在资料目录. 2.执行它,输入关键字.它回自动搜索整个目录以及下属目录中的所有文件名中包含该关键字的文件,并 ...

  5. Elastricsearch 索引操作详解(快速入门、索引管理、映射详解、索引别名)

    一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...

  6. ElasticSearch最全详细使用教程:入门、索引管理、映射详解

    墨墨导读:本文介绍了ElasticSearch的必备知识:从入门.索引管理到映射详解. 一.快速入门 1. 查看集群的健康状况http://localhost:9200/_cat http://loc ...

  7. ElasticSearch安装、IK、映射、索引管理、搜索管理和集群管理

    ElasticSearch 一.ElasticSearch 1.1 概念 1.2 原理与应用 1.2.1 索引结构 1.2.3 RESTful应用 二.ElasticSearch安装 2.1 Wind ...

  8. elasticsearch最全详细使用教程:入门、索引管理、映射详解、索引别名、分词器、文档管理、路由、搜索详解

    一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...

  9. django搜索 关键字 全文检索haystack 搜索分词数据库

    Django Haystack 简介 django-haystack 是一个专门提供搜索功能的 django 第三方应用,它支持 Solr.Elasticsearch.Whoosh.Xapian 等多 ...

最新文章

  1. Windows下的for
  2. 每日一道面试题(第7期)---Android补间动画与属性动画的区别
  3. Facebook暂停中国工具类应用广告
  4. msfpescan用法
  5. 贝叶斯告诉你,投掷硬币概率可以是90%
  6. 【转】Nginx反向代理和负载均衡
  7. lambda理解与应用
  8. 图的最小生成树(Prim算法)
  9. 95-10-170-启动-KafkaRequestHandlerPool
  10. OpenShift 4 - 用容器提升MySQL的可用性
  11. ELK下logstash通过redis收集日志
  12. Race Condition是什么
  13. 【转】轻松记住大端小端的含义(附对大端和小端的解释)
  14. luoguP1991无线通讯网
  15. Excel教程数据透视表系列案例二十二
  16. Unity 游戏实例开发集合 之 JumpJump (简单跳一跳) 休闲小游戏快速实现
  17. 油菜花系统服务器能删除内容吗,油菜花期蜂群管理要点
  18. 中国历史上10大经典遗言
  19. python中格式化输出是什么意思_python中的格式化输出用法总结
  20. Substance Painter入门

热门文章

  1. unity实验-模拟太阳系星体运动
  2. GStreamer编程笔记
  3. 织梦主动提交_织梦CMS发布文章全自动实现百度链接主动推送教程
  4. 打标激光系统的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  5. Java加解密(八)PGP协议
  6. docker实现跨宿主机的容器之间网络互联
  7. python - windows 之 mouse_event与keybd_event函数
  8. 域服务器修改主机名,域服务器主机名
  9. HummerRisk 入门3:开发手册
  10. 直流无刷电机(BLDC)应用中如何检测MOS好坏