多索引表 boost::multi_index多索引容器
Posted thefist11
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多索引表 boost::multi_index多索引容器相关的知识,希望对你有一定的参考价值。
1. 定义
一个名为multi_index_container的类模板,它支持构建容器来维护一个或多个具有不同排序和访问语义的索引。
每一个multi_index都相当于传统数据库的一个数据表(table),但将传统数据库的行与列的形式改为了单纯的列。也就是说multi_index是一个线性排列的表,只有一列,每一行都只存储一个对象。
1.1 Iterators(迭代器)
通过迭代器来操作数据表中的每个对象。
struct service_rec {
uint64_t pkey; // 主键
account_name customer; // 车主用户名
uint32_t service_date; // 维修保养时间
uint32_t odometer; // 车辆里程
};
1.2.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/shared_ptr.hpp>
using namespace std;
//定义学生信息,同理可以使用结构定义
class student {
public:
student(int id, string name, int score, string remark) :id(id), name(name), score(score), remark(remark) {
}
void print() const {
cout << "id:" << id << " name:" << name << " score:" << score << endl;
}
int id;
string name;
int score;
string remark;
};
// 如果要把student某个属性字段设置为搜索引擎,则需要定义用于排序的空结构体对象
struct _id {};
struct _name{};
struct _score {};
// 定义一个multi_index_container(多索引容器)
using student_table =
boost::multi_index::multi_index_container<
student,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<boost::multi_index::tag<_id>, BOOST_MULTI_INDEX_MEMBER(student, int, id)>, // ID为唯一索引,类似主键
boost::multi_index::ordered_non_unique<boost::multi_index::tag<_name>, BOOST_MULTI_INDEX_MEMBER(student, string, name)>, // 非唯一索引
boost::multi_index::ordered_non_unique<boost::multi_index::tag<_score>, BOOST_MULTI_INDEX_MEMBER(student, int, score)>
>
>;
int main() {
// initialize data.
student_table allStu;
allStu.insert(student(1, "lili", 85, "hello"));
allStu.insert(student(2, "liming", 90, "hello"));
allStu.insert(student(3, "xiaoming", 65, "hello"));
allStu.insert(student(4, "ergou", 80, "hello"));
allStu.insert(student(5, "dagou", 60, "hello"));
// sort
student_table::index<_id>::type& sort_id = allStu.get<0>();
cout << "sort by student id:" << endl;
student_table::index<_id>::type::iterator iter_id = sort_id.begin();
for (; iter_id != sort_id.end(); iter_id++) {
iter_id->print();
}
cout << "\\n" << endl;
student_table::index<_name>::type& sort_name = allStu.get<1>();
cout << "sort by student name:" << endl;
student_table::index<_name>::type::iterator iter_name = sort_name.begin();
for (; iter_name != sort_name.end(); iter_name++) {
iter_name->print();
}
cout << "\\n" << endl;
student_table::index<_score>::type& sort_score = allStu.get<2>();
cout << "sort by student score:" << endl;
student_table::index<_score>::type::iterator iter_score = sort_score.begin();
for (; iter_score != sort_score.end(); iter_score++) {
iter_score->print();
}
cout << "\\n" << endl;
// find
student_table::index<_name>::type& find_name = allStu.get<_name>();
student_table::index<_name>::type::iterator iter_ergou = find_name.find("ergou");
if (iter_ergou != find_name.end()) {
cout << "find a student named ergou:" << endl;
iter_ergou->print();
// modify ergou
student ergou = *iter_ergou;
ergou.id = 6; // will be success. becasuse id 6 not in the table. otherwise failure
ergou.name = "ergou_v2";
ergou.score = 91;
ergou.remark = "hello ergou";
bool isSuc = find_name.replace(iter_ergou, ergou);
}
//
cout << "sort by student id after replace ergou:" << endl;
student_table::index<_id>::type::iterator iter_id_v2 = sort_id.begin();
for (; iter_id_v2 != sort_id.end(); iter_id_v2++) {
iter_id_v2->print();
}
cout << "\\n" << endl;
system("pause");
}
输出结果:
参考:
以上是关于多索引表 boost::multi_index多索引容器的主要内容,如果未能解决你的问题,请参考以下文章