Boost multi_index_container

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;
}
#include "boost/multi_index_container.hpp"
#include "boost/multi_index/member.hpp"
#include "boost/multi_index/ordered_index.hpp"

using boost::multi_index_container;
using namespace boost::multi_index;

struct stu_num{};	// 索引-学号
struct stu_name{};  // 索引-姓名
struct stu_age{};	// 索引-年龄

typedef
boost::multi_index_container<
	Student,
	indexed_by<
		ordered_unique<
                  // 学号是唯一值的索引
		tag<stu_num>,  BOOST_MULTI_INDEX_MEMBER(Student,unsigned int,stu_num)>,
    		// 姓名是非唯一值的索引
                     ordered_non_unique<
		tag<stu_name>,BOOST_MULTI_INDEX_MEMBER(Student,std::string,stu_name)>,
                  // 年龄是非唯一值的索引
		ordered_non_unique<
		tag<stu_age>, BOOST_MULTI_INDEX_MEMBER(Student,unsigned int,stu_age)>	
	>
> StudentContainer;

	// 用名字作为索引
	StudentContainer::index<stu_name>::type& indexOfName = studentsets.get<stu_name>();

	// 查找名叫李四的人
	StudentContainer::index<stu_name>::type::iterator it = indexOfName.find("李四");

	// 找到了?
	if( it != indexOfName.end() )
	{
               // it就是一个Student序列的迭代器,现在你可以
                 // 像普通迭代器一样操作它了,比如cout << *it
	}


	// 用名字作为索引
	StudentContainer::index<stu_name>::type& indexOfName = studentsets.get<stu_name>();


	// 查找名叫张三的人的下界
	StudentContainer::index<stu_name>::type::iterator itL = indexOfName.lower_bound("张三");

	// 查找名叫张三的人的上界
	StudentContainer::index<stu_name>::type::iterator itU = indexOfName.upper_bound("张三");

         // 遍历输出所有名叫“张三”的学生信息
	while(itL != itU)
	{
		std::cout << *itL;
		++itL;
	}


Boost库中的multi_index是一种强大的工具,它允许你在STL容器(如map、set等)中创建多个索引视图,每个索引对应于不同的排序标准或访问模式。以下是使用boost::multi_index的基本步骤: 1. **包含头文件**: 首先,你需要包含`<boost/multi_index_container.hpp>`和需要的单个或多索引的头文件。 ```cpp #include <boost/multi_index_container.hpp> #include <boost/multi_index/member.hpp> ``` 2. **声明multi_index_container**: 定义一个multi_index_container,指定存储的数据类型(例如std::string),以及你想要创建的索引类型。你可以选择primary_index作为默认索引,也可以添加secondary_indices。 ```cpp using index_type = boost::multi_index::ordered_set<std::string>; typedef boost::multi_index_container< std::pair<std::string, int>, // 存储的数据类型 boost::multi_index::indexed_by< boost::multi_index::primary_index<>, boost::multi_index::hashed_non_unique<index_type> > >; ``` 这里`hashed_non_unique`表示元素是唯一的,但是查找速度较快(因为使用哈希表)。 3. **创建实例**: 实例化multi_index_container,并为其分配内存。 ```cpp index_type my_index; ``` 4. **插入数据**: 使用insert函数将数据加入容器,并关联到适当的索引。 ```cpp my_index.insert({"key1", value1}); ``` 5. **通过索引访问数据**: 可以通过索引来检索数据,如primary_index用于快速查找,而index_type可以用于按照自定义键值进行搜索。 ```cpp // Primary index search auto it = my_index.begin(); for (; it != my_index.end(); ++it) { std::cout << "Primary Index: " << it->first << ", Value: " << it->second << '\n'; } // Indexed by search auto search_result = my_index.find("key1"); if (search_result != my_index.end()) { std::cout << "Hashed Search: " << search_result->first << ", Value: " << search_result->second << '\n'; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值