转自:https://blog.csdn.net/buptman1/article/details/38657807

multi_index_container:

Boost Multi-index Containers Library定义了multi_index_container模板类,可以从不同的维度建索引、排序和存取。

如上图,容器multi_index_container分别从shape,number和sequenced(默认插入的顺序)三个维度对元素进行管理。

#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 Employee{
int id;
string name;
int age; Employee(int id_,std::string name_,int age_):id(id_),name(name_),age(age_){} friend std::ostream& operator<<(std::ostream& os,const Employee& e)
{
os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
return os;
}
}; typedef multi_index_container<
Employee,
indexed_by<
ordered_unique<member<Employee, int, &Employee::id> >,
ordered_non_unique<member<Employee, string, &Employee::name> >,
ordered_non_unique<member<Employee, int, &Employee::age> >
>
> EmployeeContainer; typedef EmployeeContainer::nth_index<0>::type IdIndex;
typedef EmployeeContainer::nth_index<1>::type NameIndex;
typedef EmployeeContainer::nth_index<2>::type AgeIndex; int main(){
EmployeeContainer con;
con.insert(Employee(0,"Joe",31));
con.insert(Employee(1,"Robert",27));
con.insert(Employee(2,"John",40)); IdIndex& ids = con.get<0>();
copy(ids.begin(),ids.end(), ostream_iterator<Employee>(cout));
cout << endl; NameIndex& names = con.get<1>();
copy(names.begin(), names.end(), ostream_iterator<Employee>(cout));
cout << endl; names.erase(names.begin()); AgeIndex& ages = con.get<2>();
copy(ages.begin(), ages.end(), ostream_iterator<Employee>(cout));
cout << endl; return 0;
}
#include "boost/multi_index_container.hpp"
#include "boost/multi_index/member.hpp"
#include "boost/multi_index/ordered_index.hpp" using boost::multi_index_container;
using namespace boost::multi_index; struct stu_num{}; // 索引-学号
struct stu_name{}; // 索引-姓名
struct stu_age{}; // 索引-年龄 typedef
boost::multi_index_container<
Student,
indexed_by<
ordered_unique<
// 学号是唯一值的索引
tag<stu_num>, BOOST_MULTI_INDEX_MEMBER(Student,unsigned int,stu_num)>,
// 姓名是非唯一值的索引
ordered_non_unique<
tag<stu_name>,BOOST_MULTI_INDEX_MEMBER(Student,std::string,stu_name)>,
// 年龄是非唯一值的索引
ordered_non_unique<
tag<stu_age>, BOOST_MULTI_INDEX_MEMBER(Student,unsigned int,stu_age)>
>
> StudentContainer;
	// 用名字作为索引
StudentContainer::index<stu_name>::type& indexOfName = studentsets.get<stu_name>(); // 查找名叫李四的人
StudentContainer::index<stu_name>::type::iterator it = indexOfName.find("李四"); // 找到了?
if( it != indexOfName.end() )
{
// it就是一个Student序列的迭代器,现在你可以
// 像普通迭代器一样操作它了,比如cout << *it
}
	// 用名字作为索引
StudentContainer::index<stu_name>::type& indexOfName = studentsets.get<stu_name>(); // 查找名叫张三的人的下界
StudentContainer::index<stu_name>::type::iterator itL = indexOfName.lower_bound("张三"); // 查找名叫张三的人的上界
StudentContainer::index<stu_name>::type::iterator itU = indexOfName.upper_bound("张三"); // 遍历输出所有名叫“张三”的学生信息
while(itL != itU)
{
std::cout << *itL;
++itL;
}
 
 

multi_index_container的更多相关文章

  1. Boost练习程序(multi_index_container)

    代码来自:http://blog.csdn.net/whuqin/article/details/8482547 该容器能实现多列索引,挺好. #include <string> #inc ...

  2. multi_index_container 多索引容器

    multi_index_container是c++ boost库中的一个多索引的容器.因工作中用到了,特来测试试用. #include "stdafx.h" #include &q ...

  3. boost multi_index

    /** boost 多索引容器的一般使用 这里使用google 的gmock 库来验证对boost 多索引模板的使用,并验证. 这里是手敲的,可能会有一些字符敲错的情况,编译错误的放,修改一下,同时链 ...

  4. Boost 1.61.0 Library Documentation

    http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...

  5. boost asio 异步实现tcp通讯

    ---恢复内容开始--- asioboost   目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化   一.前言 boost asio可算是一个简 ...

  6. 使用boost::multi_index高速构建排行榜

    使用boost::multi_index高速构建排行榜 前几天在boost的maillist上看到boost::multi_index将要支持ranked_index(邮件内容见附件2),这实乃我等苦 ...

  7. 用 boost::multi_index 管理玩家

    用 boost::multi_index 管理玩家(金庆的专栏)网游服务器上的玩家集合需要多种索引:如用ID查找,角色名查找, 用登录时分配的会话ID查找.用boost::multi_index进行玩 ...

  8. boost::multi_index 多索引容器

    #include "stdafx.h" #include <string> #include <boost/multi_index_container.hpp&g ...

  9. boost::multi_index 提供一种千人在线即时排行榜的设计思路

    原文地址: http://www.limerence2017.com/2019/06/23/cpp01/ 做游戏或金融后台开发,经常会遇到设计开发排行榜的需求.比如玩家的充值排行,战力排行等等.而这种 ...

随机推荐

  1. BG.Hive - part1

    1. Hive架构 What is hive? Facebook,https://en.wikipedia.org/wiki/Apache_Hive a> 一种工具,可以通过SQL轻松的访问数据 ...

  2. “Device eth0 does not seem to be present”解决办法

    在KVM中克隆出新的CentOS虚拟机时,出现如下问题:# service network restartShutting down loopback insterface: [ OK ]Bringi ...

  3. 泛型通配符extends与super的区别

    <? extends T>限定参数类型的上界:参数类型必须是T或T的子类型 <? super T> 限定参数类型的下界:参数类型必须是T或T的超类型 总结为: <? ex ...

  4. 关于HSQLDB访问已有数据库文件的操作说明

    关于HSQLDB数据库的创建,本文不做过多描述,可以在百度上搜索一下,有许多. 对于访问已存在的库文件,网上找了半天,没有整理的很清楚的参考资料,现将自己的操作过程整理如下,以供参考. 1.先下载一个 ...

  5. YII中利用urlManager将URL改写成restful风格

    这里主要涉及url显示样式 1.打开config文件夹下面的mian.php   2.修改内容   如把地址http://www.test.com/index.php?r=site/page/sid/ ...

  6. Git学习 之 安装

    1.官网下载 https://git-scm.com/downloads 2.修改安装目标路径,其他默认安装 3.通过系统管理员身份打开cmd,输入git 检查是否安装成功

  7. PHP isset()与empty()的使用区别

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

  8. Flink1.4.0中反序列化及序列化类变化

    Flink1.4.0中,反序列化及序列化时继承的类,有一些被标记为了“@deprecated”,路径上也有变化: 1.AbstractDeserializationSchema 以前路径 org.ap ...

  9. 润乾报表一个页面中的echarts地图与其他区块的联动

    需求概述: DBD样式效果如下图所示,需要点击左侧地图中的地区,右侧的仪表盘,柱线图可以对应显示对应该地区的数据. 实现思路: 分别制作带有地图.仪表盘.柱线图的3张报表:将3张报表放到DBD中设置布 ...

  10. U-Push 3.1.5SDK 集成的一些坑

    1.老是mPushAgent.register中onFailure获取不到deviceToken so文件配置有误,需重新配置: PushSDK .x默认只提供armeabi和x86两种so文件夹,若 ...