multi_index_container
2024-08-30 19:24:53
转自:https://blog.csdn.net/buptman1/article/details/38657807
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;
}
相关文章
- 1. 关于boost multi_index_container
- 2. boost multi_index_container 基本介绍
- 3. Boost组件multi_index_container实例(1)
- 4. Boost组件multi_index_container实例(续2)
- 5. Boost组件multi_index_container实例(续3)
- 6. Boost组件multi_index_container实例(续5)
- 7. Boost组件multi_index_container实例(续4)
- 8. Boost组件multi_index_container实例(后续)
- 9. 多索引容器boost::multi_index_container储存共享智能指针boost::shared_ptr
- 10. [HengStar-Boost讲堂]多索引容器multi_index_container实战
最新文章
- Take into Action!
- 理解C# 4 dynamic(4) – 让人惊艳的Clay
- Bumped Map And Normal Map
- 【BZOJ1968】【AHoi2005】COMMON约数研究
- MySQL主键删除/添加
- VS2010版快捷键
- HTML笔记(七)head相关元素<;base>; &; <;meta>;
- Jump Game II
- List-ApI及详解
- 一、Android NDK编程预备之Java jni简介
- hdu 4607 Park Visit(树上最长链)
- AIX6.1/11.2.0.3在有关数据库SWAP一个BUG
- Hibernate详细教程
- 如何解决js递归里面出现的堆栈溢出
- samba实现CentOS和window上的数据同步
- kindEditor 富文本编辑器 使用介绍
- Jquery 获取 radio选中值,select选中值
- FLask上传文件
- [py]Python locals() 函数
- Linux安装卸载jdk1.8