boost 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;
}



 

转载于:https://www.cnblogs.com/whuqin/archive/2013/01/08/4982021.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值