#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. MySQL第三章知识第一部分

    SQL的特点:是一个综合的.功能极强并且简洁容易学的语言. SQL的功能:数据查询.数据操纵.数据定义.数据控制. 数据库系统的主要功能是通过数据库支持的数据语言来实现. 菲关系模型(层次模型.网状模 ...

  2. [BUG]微信浏览器 iOS input 失焦页面不回弹

    描述 ios13. ios中,input唤醒软键盘后,body整体会向上滚动,如果input框输入完成确定后,如果页面在最底部则不回弹,导致fixed布局实际效果上移,fixed布局内按钮点不到. 如 ...

  3. 附014.Kubernetes Prometheus+Grafana+EFK+Kibana+Glusterfs整合性方案

    一 glusterfs存储集群部署 注意:以下为简略步骤,详情参考<附009.Kubernetes永久存储之GlusterFS独立部署>. 1.1 架构示意 略 1.2 相关规划 主机 I ...

  4. ajax js分页算法分析

    显示效果:[页面总数小于等于10,全部显示,当前页特殊显示]上一页  1   2   3   4   5   6   7   8   9   10   下一页 [页面总数大于10,部分显示,当前页特殊 ...

  5. Set-Get(?占位符)-Java(新手)

    创建实体类: package JdbcDome; public class EmpL { private int uid; private String uNAME; private int age; ...

  6. Symantec NBU :Unable to retrieve version of the server xxx.xxx.xxx

    Symantec NetBackup  是赛门铁克收购的veritas公司的一款产品,该产品功能强大,据称堪称备份界的鼻祖. 其具体原理和备份方式可见:https://blog.51cto.com/s ...

  7. VS code调试代码快速上手必备知识

    一.通过简单的配置运行一个JavaScript程序 1.打开(创建)一个新的工作空间(文件夹),并创建一个js文件: var name='world'; var s='Hello,${name}!'; ...

  8. 第十四周java实验作业

    实验十四  Swing图形界面组件 实验时间 20178-11-29 1.实验目的与要求 (1) 掌握GUI布局管理器用法: 在java中的GUI应用 程序界面设计中,布局控制通过为容器设置布局管理器 ...

  9. 如何在Linux系统上安装nginx

      安装Nginx 下载Nginx 到官网http://nginx.org/下载对应nginx包,推荐使用稳定版本进入官网之后界面如下 点击download进行到下一页 然后下载所需要的版本(强烈建议 ...

  10. Mock测试,结合Fiddler轻松搞定不同场景

    在平时测试过程中,总会遇到一些比较难构造的场景.比如不同平台间的同步,异常场景的构造.遇到难构造的场景时,就可以引用Mock来进行单元测试.简言之:mock测试就是在测试过程中,对于某些不容易构造或者 ...