#include <string>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/composite_key.hpp> using namespace boost;
using namespace boost::multi_index;
using namespace std; struct Employee{
int id;
string name;
int age; Employee():id(0),age(0){}
Employee(int id_,string name_,int age_):id(id_),name(name_),age(age_){} friend ostream& operator<<(ostream& os,const Employee& e)
{
os<<e.id<<"\t"<<e.name<<"\t"<<e.age<<endl;
return os;
} int get_id()const { return id; }
const std::string& get_name()const { return name; }
int get_age()const { return age; }
}; inline int get_student_age(const Employee &emp)
{
return emp.age;
} class ModifyEmployee
{
public:
ModifyEmployee(int id_,string name_,int age_):id(id_),name(name_),age(age_){}
void operator()(Employee &emp)
{
emp.id = id;
emp.name = name;
emp.age = age;
} private:
int id;
string name;
int age;
}; struct emp_id{};
struct emp_name{};
struct emp_age{};
struct emp_name_age{}; typedef multi_index_container<
Employee,
indexed_by< ordered_non_unique< tag<emp_name_age>,
composite_key<
Employee,
member<Employee, string, &Employee::name>,
member<Employee, int, &Employee::age> > >,
ordered_unique< tag<emp_id>, member<Employee, int, &Employee::id> >,
ordered_non_unique< tag<emp_name>, member<Employee, string, &Employee::name> >,
ordered_non_unique< tag<emp_age>, member<Employee, int, &Employee::age> >
>
> EmployeeContainer; //模板函数,用法: print_out_by<tagname>(multi_index_container_instance)
template <typename Tag, typename MultiIndexContainer>
void print_out_by(const MultiIndexContainer &s)
{
/* obtain a reference to the index tagged by Tag */ const typename boost::multi_index::index<MultiIndexContainer, Tag>::type &i = get<Tag>(s); typedef typename MultiIndexContainer::value_type value_type; /* dump the elements of the index to cout */ std::copy(i.begin(), i.end(), std::ostream_iterator<value_type>(std::cout));
} int main(){
EmployeeContainer employee;
employee.insert(Employee(1,"罗一",21));
employee.insert(Employee(2,"周二",18));
employee.get<emp_id>().insert(Employee(6,"郑六",21));
employee.insert(Employee(7,"黄七",20));
employee.insert(Employee(3,"张三",19));
employee.insert(Employee(4,"李四",28));
employee.insert(Employee(5,"李四",23));
employee.insert(Employee(8,"王八",19));
employee.insert(Employee(10,"杨十",22)); //1 打印相关信息
std::cout<<"Employee by ID"<<std::endl;
print_out_by<emp_id>(employee);
std::cout<<std::endl; std::cout<<"Employee by Name"<<std::endl;
print_out_by<emp_name>(employee);
std::cout<<std::endl; std::cout<<"Employee by Age"<<std::endl;
print_out_by<emp_age>(employee);
std::cout<<std::endl; std::cout <<"Employee by name&age" << std::endl;
print_out_by<emp_name_age>(employee); return 0;
}

boost multi_index简单了解的更多相关文章

  1. boost multi_index

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

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

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

  3. 用 boost::multi_index 管理玩家

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

  4. boost::multi_index 多索引容器

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

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

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

  6. boost multi_index 插入返回值

    boost multi_index 对象插入函数emplace() 的返回值,是一个std::pair<iterator, bool>该pair 的first 是一个插入成功的位置,第二个 ...

  7. 跨平台c++/boost/asio 简单的HTTP POST请求 客户端模型

    作为一个呼应,写一个c++版本的同步http post客户端功能,如果你需要纯C版本,移步这里 linux下纯C简单的HTTP POST请求 客户端模型 讲解一下基本的的http post协议 通过\ ...

  8. 使用 boost.asio 简单实现 异步Socket 通信

     客户端: class IPCClient { public: IPCClient(); ~IPCClient(); bool run(); private: bool connect(); bool ...

  9. boost asio 异步实现tcp通讯

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

随机推荐

  1. python关于字典如何格式化地写入文件之中

    1.python关于字典如何式化地写入文件之中 如何写入:https://blog.csdn.net/qq_15642411/article/details/79943741 (推荐使用json包) ...

  2. 解决Requires: libc.so.6(GLIBC_2.14)(64bit)错误解决方法

    glibc简介: glibc是GNU发布的libc库,即c运行库.glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibc.glibc除了封装linux操作系统所提供的系统服 ...

  3. IOS抓包工具Stream——让移动端的抓包变得轻而易举

    有一天下晚班回家,在地铁上的时候,开发发来信息说,能不能把之前创建的bug再抓包看下数据.顿时心里就想,在地铁上,我上哪抓包去.之后百度了下,发现ios有一款非常实用的抓包工具,大家可以上App St ...

  4. xargs命令_Linux xargs命令:一个给其他命令传递参数的过滤器

    本文要为大家介绍的命令是 xargs,我们把它称为护花使者,因为它总是乐于协助其他的命令来完成一些事情.下面一起来看看它是如何护花的. xargs 是 execute arguments 的缩写,它的 ...

  5. input标签的accept属性、JQuery绑定keyDown事件

    一. input标签的accept属性 当我们上传文件或者注册上传头像时,我们可以一般都是使用: <input type="file" id="my_file&qu ...

  6. 【codeforces】Educational Codeforces Round 80 D. Minimax Problem——二分+二进制处理

    题目链接 题目大意 有n个维度为m的向量,取其中两个进行合并,合并时每个维度取两者之间的较大者,得到的新的向量中,维度值最小者最大为多少 分析 首先最需要注意的是m的取值,m最大只有8 那么我们可以二 ...

  7. python-文本字符串

    2019-12-05 14:41:36 一.Unicode 编码问题一直都是文本处理的时候的大难题,python2中的编码异常混乱,本章节主要讨论python3中的编码情况. python3 str的 ...

  8. Prism 源码解读3-Modules加载

    介绍 在软件开发过程中,总想组件式的开发方式,各个组件之间最好互不影响,独立测试.Prism的Modules很好的满足了这一点. 这个架构图很好了讲解了Prism的Modules的概念 Prism支持 ...

  9. 字节转换函数 htonl*的由来与函数定义...

    字节转换字符由来: 在网络上面有着许多类型的机器,这些机器在表示数据的字节顺序是不同的, 比如i386芯片是低字节在内存地址的低端, intel处理器将32位的整数分4个连续的字节,并以字节序1-2- ...

  10. OpenCV-Python 读取显示图像 | 五

    目标 在这里,你将学习如何读取图像,如何显示图像以及如何将其保存回去 你将学习以下功能:cv.imread(),cv.imshow(),cv.imwrite() (可选)你将学习如何使用Matplot ...