#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. (11)nc命令(每周一个linux命令)

    nc(netcat)实用程序几乎可用于所有涉及TCP或UDP的事情.它可以打开TCP连接,发送UDP数据包,监听任意TCP和UDP端口,进行端口扫描,处理IPv4和IPv6.与telnet不同,nc可 ...

  2. 三万字、91道MySQL面试题。 附PDF

    文末领取面试题 高清PDF 数据库基础知识 1. 为什么要使用数据库 数据保存在内存 优点:存取速度快 缺点:数据不能永久保存 数据保存在文件 优点:数据永久保存 缺点:1)速度比内存操作慢,频繁的I ...

  3. Peek和Pop功能开发(3D Touch开发之一)

    1.哪些设备支持3D Touch iPhone 6s/Plus.iPhone 7s/Plus.iPhone 8s/Plus.iPhone X (系统必须是iOS9或者更新的版本) 2.怎么实现Peek ...

  4. 新手版超详细LoadRunner12完整安装+汉化过程

      01下载 首先从百度网盘获取到这几个文件(网盘地址会附在文末,过期请联系): 我安装的是社区版+中文汉化过的,使用我只下载了第一个和第三个文件,下面我将讲一下如何安装. 02安装社区版 1.选择“ ...

  5. eNSP 交换机 路由器 PC 互连设计/实现

    0.实验目的 1.掌握网络设计的原理与步骤: 2.掌握IP分配.网关设置原则: 3.了解路由协议的作用,掌握网络互联设备的作用和配置. 1.实验环境 环境:eNSP模拟器 版本信息:1.3.00.10 ...

  6. [JVM教程与调优] JVM都有哪些参数类型?

    JDK本身是提供了一些监控工具,有一些是命令行,也有图形界面.本次介绍命令行如何进行监控. 命令行是非常重要的,因为在我们生产环境基本上是没有图形界面的,完全是通过命令行. 主要内容: JVM的参数类 ...

  7. Building Applications with Force.com and VisualForce (DEV401) 中用到的Recruiting Application介绍

    1.Who uses Recruiting Application. 2. Recruiting Application Object Model

  8. OpenCV-Python 直方图-2:直方图均衡 | 二十七

    目标 在本节中, 我们将学习直方图均衡化的概念,并利用它来提高图像的对比度. 理论 考虑这样一个图像,它的像素值仅局限于某个特定的值范围.例如,较亮的图像将把所有像素限制在高值上.但是一幅好的图像会有 ...

  9. 一文看懂NLP神经网络发展历史中最重要的8个里程碑!

    导读:这篇文章中作者尝试将 15 年的自然语言处理技术发展史浓缩为 8 个高度相关的里程碑事件,不过它有些偏向于选择与当前比较流行的神经网络技术相关的方向.我们需要关注的是,本文中介绍的许多神经网络模 ...

  10. SG函数(斐波那契博弈) Fibonacci again and again

    https://zhuanlan.zhihu.com/p/53948422 HDU - 1848 将这篇文章认真的看了一遍 ,虽然不是很懂 ,但是脑子里有了一个模型,链接里的图 (看的顺序 是 0,1 ...