根据不同的索引排序结构体。其中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. 网络工程与机房等精华指引贴
  2. python读写csv确定编码格式_Python使用utf8编码读写csv文件
  3. sql server管理学习提纲
  4. 不使用第三个变量交换两个变量的值
  5. 控制器及其中$scope
  6. mysql中不要 秒的函数_Mysql中日期和时间函数应用不用求人 | 很文博客
  7. 忽略git项目上的任何#39;bin#39;目录
  8. 树莓派模拟电路_基于树莓派的热电偶测量模块 MCC 134
  9. Dubbo分析之Registry层
  10. python md5解密_Python md5解密
  11. Linux下ALSA驱动分析
  12. 述职答辩提问环节一般可以问些什么_答辩时老师一般会提问哪些?
  13. 2022年起重机械指挥特种作业证考试题库及答案
  14. 值得重视的网络安全问题
  15. 华为交换机关闭网口_定时关闭华为交换机的端口
  16. SPI串行外围设备接口
  17. 英文歌曲:God is a girl(上帝是女孩)
  18. Github pages个人域名添加SSL
  19. Java使用EasyExcel下载xls、xlsx 出现文件格式与扩展名不匹配(亲测)
  20. MT8788/MT6771/MT8766硬件相同怎样实现单双卡共用同一个image

热门文章

  1. Ajax的五种接收响应头消息(常用)
  2. 对豆瓣《战狼2》87767 条短评做词云
  3. 医保业务综合服务终端技术规范_泰山区:82个“泰好办”自助服务终端办理点方便居民就近办业务...
  4. IP协议的服务类型(翻译RFC 1349)
  5. 欧姆龙PLC ST语言6轴伺服RS232C通讯板CP1W-C IF0 真实项目程序,ST语言写的FB块
  6. Warning: PHP Startup: Unable to load dynamic library 'D:\phpS p\php-7.0.12-nts\ext\php_igbinary.dll'
  7. python3 translate---TypeError: translate() takes exactly one argument (2 given)
  8. 安卓开发 切换简繁体
  9. unbuntu中C语言环境安装
  10. 你为什么还在坚持玩《我的世界》?如何搭建《我的世界》服务器?