1. 定义

一个名为multi_index_container的类模板,它支持构建容器来维护一个或多个具有不同排序和访问语义的索引。

每一个multi_index都相当于传统数据库的一个数据表(table),但将传统数据库的行与列的形式改为了单纯的列。也就是说multi_index是一个线性排列的表,只有一列,每一行都只存储一个对象。

1.1 Iterators(迭代器)

通过迭代器来操作数据表中的每个对象。

struct service_rec {uint64_t        pkey;           // 主键account_name    customer;       // 车主用户名uint32_t        service_date;   // 维修保养时间uint32_t        odometer;       // 车辆里程};

1.2.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/shared_ptr.hpp>using namespace std;//定义学生信息,同理可以使用结构定义
class student {public:student(int id, string name, int score, string remark) :id(id), name(name), score(score), remark(remark) {}void print() const {cout << "id:" << id << " name:" << name << " score:" << score << endl;}int id;string name;int score;string remark;
};// 如果要把student某个属性字段设置为搜索引擎,则需要定义用于排序的空结构体对象
struct _id {};
struct _name{};
struct _score {};// 定义一个multi_index_container(多索引容器)
using student_table =
boost::multi_index::multi_index_container<student,boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<_id>, BOOST_MULTI_INDEX_MEMBER(student, int, id)>,   // ID为唯一索引,类似主键boost::multi_index::ordered_non_unique<boost::multi_index::tag<_name>, BOOST_MULTI_INDEX_MEMBER(student, string, name)>, // 非唯一索引boost::multi_index::ordered_non_unique<boost::multi_index::tag<_score>, BOOST_MULTI_INDEX_MEMBER(student, int, score)>>
>;int main() {// initialize data.student_table allStu;allStu.insert(student(1, "lili", 85, "hello"));allStu.insert(student(2, "liming", 90, "hello"));allStu.insert(student(3, "xiaoming", 65, "hello"));allStu.insert(student(4, "ergou", 80, "hello"));allStu.insert(student(5, "dagou", 60, "hello"));// sortstudent_table::index<_id>::type& sort_id = allStu.get<0>();cout << "sort by student id:" << endl;student_table::index<_id>::type::iterator iter_id = sort_id.begin();for (; iter_id != sort_id.end(); iter_id++) {iter_id->print();}cout << "\n" << endl;student_table::index<_name>::type& sort_name = allStu.get<1>();cout << "sort by student name:" << endl;student_table::index<_name>::type::iterator iter_name = sort_name.begin();for (; iter_name != sort_name.end(); iter_name++) {iter_name->print();}cout << "\n" << endl;student_table::index<_score>::type& sort_score = allStu.get<2>();cout << "sort by student score:" << endl;student_table::index<_score>::type::iterator iter_score = sort_score.begin();for (; iter_score != sort_score.end(); iter_score++) {iter_score->print();}cout << "\n" << endl;// findstudent_table::index<_name>::type& find_name = allStu.get<_name>();student_table::index<_name>::type::iterator iter_ergou = find_name.find("ergou");if (iter_ergou != find_name.end()) {cout << "find a student named ergou:" << endl;iter_ergou->print();// modify ergoustudent ergou = *iter_ergou;ergou.id = 6;            // will be success. becasuse id 6 not in the table. otherwise failureergou.name = "ergou_v2";ergou.score = 91;ergou.remark = "hello ergou";bool isSuc = find_name.replace(iter_ergou, ergou);}// cout << "sort by student id after replace ergou:" << endl;student_table::index<_id>::type::iterator iter_id_v2 = sort_id.begin();for (; iter_id_v2 != sort_id.end(); iter_id_v2++) {iter_id_v2->print();}cout << "\n" << endl;system("pause");
}

输出结果:

参考:

  1. boost::multi_index多索引容器-使用说明

多索引表 (1)boost::multi_index多索引容器相关推荐

  1. 数据结构-串操作应用之词索引表

    为书库创建查询索引表 建立词索引表基本步骤: 1.从书目文件中读入一个书目单. 2.从书目单中提取所有关键字插入词表. 3.对词表中的每一个关键字在索引表中进行查找并作相应的插入操作. 详细操作: 1 ...

  2. 数据结构 单词查找 二分法-索引表-Hash表

    问题说明: 从标准输入中读入一个英文单词及查找方式,在一个给定的英文常用单词字典文件dictionary3000.txt中查找该单词,返回查找结果(查找到返回1,否则返回0)和查找过程中单词的比较次数 ...

  3. 云计算设计模式(十二)——索引表模式

    云计算设计模式(十二)--索引表模式 创建索引过的被查询条件经常被引用的数据存储等领域.这种模式可以通过允许应用程序更快速地定位数据来从数据存储中检索提高查询性能. 背景和问题 许多数据存储通过使用主 ...

  4. HBase 索引表结构

    1. 索引表的结构 在HBase中,表格的Rowkey按照字典排序,Region按照RowKey设置split point进行shard,通过这种方式实现的全局.分布式索引,成为了其成功的最大的砝码 ...

  5. 索引表和ES的一点点思考

    索引表设计 在电商项目中,物理库存系统是个极其重要的系统,订单支付后,就会开始来占用物理库存.一般情况下,库存系统都是要分库的,因为主要的操作是写操作,例如占用/释放/取消等写操作.使用分库可以降低数 ...

  6. 聚集索引和非聚集索引的区别底层_数据库-索引相关

    一.什么是索引 在数据库中,索引的含义与日常意义上的"索引"一词并无多大区别(想想小时候查字典),它是用于提高数据库表数据访问速度的数据库对象. 总而言之,索引是一个排序的列表,在 ...

  7. boost multi_index_container 多索引容器的使用

    多索引容器 multi_index_container 为什么要使用multi_index_container 相信想必大家在实际开发中一定多少会遇到一些的问题,我需要创建一个可以索引的容器,例如ma ...

  8. Oracle 表的访问方式(2)-----索引扫描

    索引扫描(Index scan) 我们先通过index查找到数据对应的rowid值(对于非唯一索引可能返回多个rowid值),然后根据rowid直接从表中得到具体的数据,这种查找方式称为索引扫描或索引 ...

  9. wukong引擎源码分析之搜索——docid有序的数组里二分归并求交集,如果用跳表的话,在插入索引时会更快...

    searcher.Search(types.SearchRequest{Text: "百度中国"}) // 查找满足搜索条件的文档,此函数线程安全 func (engine *En ...

最新文章

  1. 矢量旋度的散度恒为零
  2. 现代人,特别需要蒸之一
  3. 64位进程调用32位dll的解决方法
  4. 深度学习(2)--常见概率分布(1)
  5. MySQL备份失败,一波三折的问题分析和处理
  6. Linux系统简介与准备
  7. html+css+js中的小知识点汇聚(无序 停更)
  8. zabbix安装与配置
  9. ubuntu升级tensorflow版本
  10. a 和an 的用法区别
  11. 《Introduction To Modern Cryptography》读书笔记一
  12. 从零开始学USB(十五、USB的设备状态)
  13. Windows11显示网速
  14. Gstreamer- 协商(Negotiation)
  15. 关于无法安装.NET Framework3.5的问题(0x800F081F)
  16. python编写木马攻击_Python编写简易木马程序
  17. DevOps在证券互联网研发中的应用与实践
  18. jmeter性能测试并监控服务器
  19. 设计模式第10式:状态模式
  20. 频率与周期的对应关系

热门文章

  1. Android --- TabLayout 切换时,改变选项卡下字体的状态(大小、加粗、默认被选中第一个)
  2. 联想v3500存储Linux配置手册,OEL6.X IBM v3500存储多路径配置
  3. java随机数转换_Java获取随机数的3种方法(转)
  4. jqgrid 服务器端验证,jqGrid获取服务服务器返回的所有数据
  5. 展望数据中心市场未来十年发展趋势
  6. 绿色运营,数据中心还得靠自动化
  7. vue 获取当前位置 高德_vue高德地图获取当前位置
  8. Database之SQLSever:SQLSever数据库管理学习并深入理解SQL命令语句进阶综合篇《初级→中级→高级》(持续更新,建议收藏)
  9. Python语言学习之pandas:DataFrame二维表的简介、常用函数、常用案例之详细攻略
  10. Py之cx_Freeze:Python库之cx_Freeze库(程序打包)简介、安装、使用方法详细攻略—案例之实现机器人在线24小时智能翻译