多索引表 (1)boost::multi_index多索引容器
生活随笔
收集整理的這篇文章主要介紹了
多索引表 (1)boost::multi_index多索引容器
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 定義
一個(gè)名為multi_index_container的類(lèi)模板,它支持構(gòu)建容器來(lái)維護(hù)一個(gè)或多個(gè)具有不同排序和訪問(wèn)語(yǔ)義的索引。
每一個(gè)multi_index都相當(dāng)于傳統(tǒng)數(shù)據(jù)庫(kù)的一個(gè)數(shù)據(jù)表(table),但將傳統(tǒng)數(shù)據(jù)庫(kù)的行與列的形式改為了單純的列。也就是說(shuō)multi_index是一個(gè)線性排列的表,只有一列,每一行都只存儲(chǔ)一個(gè)對(duì)象。
1.1 Iterators(迭代器)
通過(guò)迭代器來(lái)操作數(shù)據(jù)表中的每個(gè)對(duì)象。
struct service_rec {uint64_t pkey; // 主鍵account_name customer; // 車(chē)主用戶名uint32_t service_date; // 維修保養(yǎng)時(shí)間uint32_t odometer; // 車(chē)輛里程};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;//定義學(xué)生信息,同理可以使用結(jié)構(gòu)定義 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某個(gè)屬性字段設(shè)置為搜索引擎,則需要定義用于排序的空結(jié)構(gòu)體對(duì)象 struct _id {}; struct _name{}; struct _score {};// 定義一個(gè)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為唯一索引,類(lèi)似主鍵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"));// sortstudent_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;// findstudent_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 ergoustudent ergou = *iter_ergou;ergou.id = 6; // will be success. becasuse id 6 not in the table. otherwise failureergou.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"); }輸出結(jié)果:
參考:
總結(jié)
以上是生活随笔為你收集整理的多索引表 (1)boost::multi_index多索引容器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++ Primer 5th笔记(cha
- 下一篇: 多索引表 (2)基本概念