boost多个关键字索引multi_index_container
根据不同的索引排序结构体。其中tag的意思是指定一个标记,如果不指定的话默认是从0开始,以下例子展示了这两种情况
代码:
- #include <string>
- #include <iostream>
- #include <boost/multi_index_container.hpp>
- #include <boost/multi_index/member.hpp>
- #include <boost/multi_index/ordered_index.hpp>
- using namespace boost;
- using namespace boost::multi_index;
- using namespace std;
- struct Person{
- int id;
- int age;
- int height;
- string name;
- Person(int id_,int age_,int height_,std::string name_):
- id(id_),
- age(age_),
- height(height_),
- name(name_)
- {}
- };
- std::ostream& operator<<(std::ostream& os,const Person& ps)
- {
- os<<ps.id<<" "<<ps.age<<" "<<ps.height<<" "<<ps.name<<" "<<std::endl;
- return os;
- }
- typedef multi_index_container<
- Person,
- indexed_by<
- ordered_unique<member<Person, int, &Person::id> >,
- ordered_non_unique<member<Person, int, &Person::age> >,
- ordered_non_unique<member<Person, int, &Person::height> >,
- ordered_non_unique<member<Person, string, &Person::name> >
- > >PersonContainer;
- typedef PersonContainer::nth_index<0>::type IdIndex;
- typedef PersonContainer::nth_index<1>::type AgeIndex;
- typedef PersonContainer::nth_index<2>::type HeightIndex;
- typedef PersonContainer::nth_index<3>::type NameIndex;
- struct person_id{};
- struct person_age{};
- struct person_height{};
- struct person_name{};
- typedef multi_index_container<
- Person,indexed_by<
- ordered_unique< tag<person_id>,member<Person, int, &Person::id> >,
- ordered_non_unique< tag<person_age>,member<Person, int, &Person::age> >,
- ordered_non_unique< tag<person_height>,member<Person, int, &Person::height> >,
- ordered_non_unique<tag<person_name>,member<Person, string, &Person::name> >
- > >PersonContainerTag;
- int main(){
- PersonContainer con;
- con.insert(Person(2,31,170,"aliu"));
- con.insert(Person(1,27,164,"dliu"));
- con.insert(Person(0,55,182,"cliu"));
- con.insert(Person(3,51,142,"bliu"));
- IdIndex& ids = con.get<0>();
- copy(ids.begin(),ids.end(), ostream_iterator<Person>(cout));
- cout << endl;
- AgeIndex& ages = con.get<1>();
- copy(ages.begin(), ages.end(), ostream_iterator<Person>(cout));
- cout << endl;
- HeightIndex& heights = con.get<2>();
- copy(heights.begin(), heights.end(), ostream_iterator<Person>(cout));
- cout << endl;
- NameIndex& names = con.get<3>();
- copy(names.begin(), names.end(), ostream_iterator<Person>(cout));
- cout << endl;
- PersonContainerTag con_tag;
- con_tag.insert(Person(2,31,170,"aliu"));
- con_tag.insert(Person(1,27,164,"dliu"));
- con_tag.insert(Person(0,55,182,"cliu"));
- con_tag.insert(Person(3,51,142,"bliu"));
- auto& ids_tag = con_tag.get<person_id>();
- copy(ids_tag.begin(),ids_tag.end(), ostream_iterator<Person>(cout));
- cout << endl;
- auto& ages_tag = con_tag.get<person_age>();
- copy(ages_tag.begin(), ages_tag.end(), ostream_iterator<Person>(cout));
- cout << endl;
- auto& heights_tag = con_tag.get<person_height>();
- copy(heights_tag.begin(), heights_tag.end(), ostream_iterator<Person>(cout));
- cout << endl;
- auto& names_tag = con_tag.get<person_name>();
- copy(names_tag.begin(), names_tag.end(), ostream_iterator<Person>(cout));
- cout << endl;
- return 0;
- }
结果:
- 0 55 182 cliu
- 1 27 164 dliu
- 2 31 170 aliu
- 3 51 142 bliu
- 1 27 164 dliu
- 2 31 170 aliu
- 3 51 142 bliu
- 0 55 182 cliu
- 3 51 142 bliu
- 1 27 164 dliu
- 2 31 170 aliu
- 0 55 182 cliu
- 2 31 170 aliu
- 3 51 142 bliu
- 0 55 182 cliu
- 1 27 164 dliu
- 0 55 182 cliu
- 1 27 164 dliu
- 2 31 170 aliu
- 3 51 142 bliu
- 1 27 164 dliu
- 2 31 170 aliu
- 3 51 142 bliu
- 0 55 182 cliu
- 3 51 142 bliu
- 1 27 164 dliu
- 2 31 170 aliu
- 0 55 182 cliu
- 2 31 170 aliu
- 3 51 142 bliu
- 0 55 182 cliu
- 1 27 164 dliu
boost多个关键字索引multi_index_container相关推荐
- Boost.MultiIndex 使用序列索引的示例
Boost.MultiIndex 使用序列索引的示例 实现功能 C++实现代码 实现功能 Boost.MultiIndex 使用序列索引的示例 C++实现代码 #if !defined(NDEBUG) ...
- 使用 [funcref boost::pfr::get] 按索引访问结构体字段的测试程序
使用 [funcref boost::pfr::get] 按索引访问结构体字段的测试程序 实现功能 C++实现代码 实现功能 使用 [funcref boost::pfr::get] 按索引访问结构体 ...
- redis zset转set 反序列化失败_Redis只往zset有序集合添加不存在的数据:关键字索引查询构建+源码分析...
Redis的有序集合Sorted Set(zset),可以很方便地用来构建关键字索引表,可以很方便地实现支持超大规模并发的关键字组合条件查询. 比如有套博客系统,博客文章存放在 hash 类型 art ...
- 一个自动生成关键字索引页面的比处理文件
电脑上资料多了之后,每次找资料很麻烦.因此,匠人写了这个批处理文件. 1.把这个批处理文件放在资料目录. 2.执行它,输入关键字.它回自动搜索整个目录以及下属目录中的所有文件名中包含该关键字的文件,并 ...
- Elastricsearch 索引操作详解(快速入门、索引管理、映射详解、索引别名)
一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...
- ElasticSearch最全详细使用教程:入门、索引管理、映射详解
墨墨导读:本文介绍了ElasticSearch的必备知识:从入门.索引管理到映射详解. 一.快速入门 1. 查看集群的健康状况http://localhost:9200/_cat http://loc ...
- ElasticSearch安装、IK、映射、索引管理、搜索管理和集群管理
ElasticSearch 一.ElasticSearch 1.1 概念 1.2 原理与应用 1.2.1 索引结构 1.2.3 RESTful应用 二.ElasticSearch安装 2.1 Wind ...
- elasticsearch最全详细使用教程:入门、索引管理、映射详解、索引别名、分词器、文档管理、路由、搜索详解
一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...
- django搜索 关键字 全文检索haystack 搜索分词数据库
Django Haystack 简介 django-haystack 是一个专门提供搜索功能的 django 第三方应用,它支持 Solr.Elasticsearch.Whoosh.Xapian 等多 ...
最新文章
- 网络工程与机房等精华指引贴
- python读写csv确定编码格式_Python使用utf8编码读写csv文件
- sql server管理学习提纲
- 不使用第三个变量交换两个变量的值
- 控制器及其中$scope
- mysql中不要 秒的函数_Mysql中日期和时间函数应用不用求人 | 很文博客
- 忽略git项目上的任何#39;bin#39;目录
- 树莓派模拟电路_基于树莓派的热电偶测量模块 MCC 134
- Dubbo分析之Registry层
- python md5解密_Python md5解密
- Linux下ALSA驱动分析
- 述职答辩提问环节一般可以问些什么_答辩时老师一般会提问哪些?
- 2022年起重机械指挥特种作业证考试题库及答案
- 值得重视的网络安全问题
- 华为交换机关闭网口_定时关闭华为交换机的端口
- SPI串行外围设备接口
- 英文歌曲:God is a girl(上帝是女孩)
- Github pages个人域名添加SSL
- Java使用EasyExcel下载xls、xlsx 出现文件格式与扩展名不匹配(亲测)
- MT8788/MT6771/MT8766硬件相同怎样实现单双卡共用同一个image
热门文章
- Ajax的五种接收响应头消息(常用)
- 对豆瓣《战狼2》87767 条短评做词云
- 医保业务综合服务终端技术规范_泰山区:82个“泰好办”自助服务终端办理点方便居民就近办业务...
- IP协议的服务类型(翻译RFC 1349)
- 欧姆龙PLC ST语言6轴伺服RS232C通讯板CP1W-C IF0 真实项目程序,ST语言写的FB块
- Warning: PHP Startup: Unable to load dynamic library 'D:\phpS p\php-7.0.12-nts\ext\php_igbinary.dll'
- python3 translate---TypeError: translate() takes exactly one argument (2 given)
- 安卓开发 切换简繁体
- unbuntu中C语言环境安装
- 你为什么还在坚持玩《我的世界》?如何搭建《我的世界》服务器?