转自: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. HDU 5253 连接的管道(Kruskal算法求解MST)

    题目: 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行灌溉了.当老 J ...

  2. 4.1 SQL的本质

    对于早期的关系数据库,整个行业做了很多努力,试图统一不同的专用查询语言.IBM曾建立了一个早期的标准,被称为Structured English Query Language,这个名字缩写为SEQUE ...

  3. SQL 修改表字段失败 解决方法

    OK  大功告成 !!!

  4. 用bind方法保持this上下文

    什么是this对象 先来说说什么是this对象吧.每一个函数在调用的时候都会自己主动获取两个特殊变量:this和arguments对象. this值详细是指哪个对象是和该函数的运行环境相关的.假设是作 ...

  5. 一分钟配置jdk

    一.下载jdk 直接进入网站:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...

  6. 关于在web端运行项目,eclipse报PermGen space错误

    之前在网上也查到过许多解决的方法,但可能因为本人脸黑........也修改过eclipse文件目录中的相关配置文件,并没有得到相应的帮助,因此把自己的改正方法分享下: window-->pref ...

  7. spring 中 InitializingBean 接口使用理解

    前言:这两天在看 spring 与 quart 的集成,所以了解到 spring 是如何初始化 org.springframework.scheduling.quartz.SchedulerFacto ...

  8. K:Treap(堆树)

      Treap=Tree+Heap.Treap是一棵二叉排序树,它的左子树和右子树分别是一个Treap,和一般的二叉排序树不同的是, Treap记录一个额外的数据, 就是优先级.Treap在以关键码构 ...

  9. 设计模式学习——工厂模式(Factory Pattern)

    1.有一个工厂,专门生产不同品牌的汽车.当有人需要从此工厂提货的时候,只需要告诉他,要什么品牌的,就可以了,并不关心这些车是怎么生产出来的. 2.以上方式,如果增加品牌的时候,也要修改工厂,有点麻烦. ...

  10. 自动补全Typeahead

    采用 Typeahead (Bootstrap-3-Typeahead-master) <script type="text/javascript" src="/j ...