boost multi_index简单了解
#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简单了解的更多相关文章
- boost multi_index
/** boost 多索引容器的一般使用 这里使用google 的gmock 库来验证对boost 多索引模板的使用,并验证. 这里是手敲的,可能会有一些字符敲错的情况,编译错误的放,修改一下,同时链 ...
- 使用boost::multi_index高速构建排行榜
使用boost::multi_index高速构建排行榜 前几天在boost的maillist上看到boost::multi_index将要支持ranked_index(邮件内容见附件2),这实乃我等苦 ...
- 用 boost::multi_index 管理玩家
用 boost::multi_index 管理玩家(金庆的专栏)网游服务器上的玩家集合需要多种索引:如用ID查找,角色名查找, 用登录时分配的会话ID查找.用boost::multi_index进行玩 ...
- boost::multi_index 多索引容器
#include "stdafx.h" #include <string> #include <boost/multi_index_container.hpp&g ...
- boost::multi_index 提供一种千人在线即时排行榜的设计思路
原文地址: http://www.limerence2017.com/2019/06/23/cpp01/ 做游戏或金融后台开发,经常会遇到设计开发排行榜的需求.比如玩家的充值排行,战力排行等等.而这种 ...
- boost multi_index 插入返回值
boost multi_index 对象插入函数emplace() 的返回值,是一个std::pair<iterator, bool>该pair 的first 是一个插入成功的位置,第二个 ...
- 跨平台c++/boost/asio 简单的HTTP POST请求 客户端模型
作为一个呼应,写一个c++版本的同步http post客户端功能,如果你需要纯C版本,移步这里 linux下纯C简单的HTTP POST请求 客户端模型 讲解一下基本的的http post协议 通过\ ...
- 使用 boost.asio 简单实现 异步Socket 通信
客户端: class IPCClient { public: IPCClient(); ~IPCClient(); bool run(); private: bool connect(); bool ...
- boost asio 异步实现tcp通讯
---恢复内容开始--- asioboost 目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化 一.前言 boost asio可算是一个简 ...
随机推荐
- Spring注解 - AOP 面向切面编程
基本概念: AOP:Aspect Oriented Programming,即面向切面编程 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式 前置通知(@Before):在目标 ...
- 使用Netty如何解决拆包粘包的问题
首先,我们通过一个DEMO来模拟TCP的拆包粘包的情况:客户端连续向服务端发送100个相同消息.服务端的代码如下: AtomicLong count = new AtomicLong(0); NioE ...
- Natas6 Writeup(PHP Include)
Natas6: 该题提供了php源码,点击查看分析,发现调用了includes/secret.inc页面,在输入一个变量secret后,如果和includes/secret.inc中 预设的secre ...
- Arch Linux开启SSH远程安装(1.5)
现在你的眼前应该可以看到[root@archiso~]#的提示. 首先,建立目标机器的网络设置: 安装和升级软件包前,先让本地的包数据库和远程的软件仓库同步是个好习惯. [root@archiso~] ...
- Android 引导页的代码
布局代码 <android.support.v4.view.ViewPager android:id="@+id/viewpage" android:layout_width ...
- python浅学【网络服务中间件】之MongoDB
一.关于MongoDB: MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供 ...
- 二进制补码:Why & How
二进制补码:Why & How 学习计算机原理或者语言的底层操作难免会遇到用二进制补码表示负数的问题.由于一些书本上对于采用补码的原因没有详细解释,很多人会认为这只是一种规定,但实际上采用补码 ...
- 题解 SP2916 【GSS5 - Can you answer these queries V】
前言 最近沉迷于数据结构,感觉数据结构很有意思. 正文 分析 先来分类讨论一下 1. \(x2<y1\) 如果 \(y1<x2\) 的话,答案 \(=\max \limits_{ y1 \ ...
- 写爬虫爬了3w条职位数据,看看当前招聘形势 | 开源
最近有不少程序员又开始找工作了,为了了解目前技术类各职位的数量.薪资.招聘公司.岗位职责及要求,我爬取了拉勾网北上广深4个城市的招聘数据,共3w条.职位包括:人工智能(AI).大数据.数据分析.后端( ...
- MySQL到底能有多少个字段
今天技术讨论群里 “一切随遇而安”同学看书时出现一个疑问,一个MySQL的表中到底可以有多少个字段?带着这个疑问,我们展开了探讨,也接着讨论了一个单字段长度的问题. 1. 官方文档说明 官方文档的内 ...