首页 > 其他分享 >boost多索引容器的使用介绍

boost多索引容器的使用介绍

时间:2023-09-15 16:46:10浏览次数:64  
标签:容器 remark name hello 索引 score student boost id

boost是基于C++11的被广泛使用的开源库;
多索引容器实现了可以通过多个索引去查找数据,不像std::map 一样 只能通过单一索引key查找对应value值。多索引容器可以通过多个索引key查找对应value值。多对多的方式,有了更多的应用场景。其实现了STL的一些基本操作,如迭代器等。如下示例作为对多索引容器的进一步了解。
boost编译见 https://blog.csdn.net/weixin_44328568/article/details/128011177

示例 multi_index.cpp:

#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 << "\tid:" << id << "\tname:" << name << "\tscore:" << score << "\tremark:" << remark << 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)>
        >
>;


void insertData( 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"));
}
/* compile:  g++ -std=c++11 -I./include multi_index.cpp */
int main() {
        student_table allStu;

        insertData( allStu ); // 插入数据

        cout << endl << "-- sort by student id:" << endl << endl;
        for (auto &iter : allStu.get<_id>() ){ // get return of student_table::index<_id>::type&
                iter.print();
        }

        cout << endl << "-- sort by student name:" << endl << endl;
        for (auto &iter : allStu.get<_name>() ){ // get return of student_table::index<_name>::type&
                iter.print();
        }

        cout << endl << "-- sort by student score:" << endl << endl;
        for (auto &iter_score : allStu.get<_score>()) { // student_table::index<_score>::type&
                iter_score.print();
        }

        // find and modify
        auto &stu_of_name  = allStu.get<_name>(); // student_table::index<_name>::type
        auto iter_ergou = stu_of_name.find("ergou"); // student_table::index<_name>::type::iterator

        if ( iter_ergou != stu_of_name.end() ) {
                student ergou = *iter_ergou;
                ergou.score = 33;
                ergou.remark = "modified by program";
                bool isSuc = stu_of_name.replace(iter_ergou, ergou);
        }

        cout << endl << "-- sort by student id after replace ergou:" << endl << endl;
        for (auto &iter : allStu.get<0>() ){ // get return of student_table::index<_id>::type&
                iter.print();
        }

        cout << endl;
}

编译及结果:

-bash-4.2# ls
include  lib  multi_index.cpp
-bash-4.2# g++ -std=c++11 -I./include multi_index.cpp
-bash-4.2# ./a.out

-- sort by student id:

        id:1    name:lili       score:85        remark:hello
        id:2    name:liming     score:90        remark:hello
        id:3    name:xiaoming   score:65        remark:hello
        id:4    name:ergou      score:80        remark:hello
        id:5    name:dagou      score:60        remark:hello

-- sort by student name:

        id:5    name:dagou      score:60        remark:hello
        id:4    name:ergou      score:80        remark:hello
        id:1    name:lili       score:85        remark:hello
        id:2    name:liming     score:90        remark:hello
        id:3    name:xiaoming   score:65        remark:hello

-- sort by student score:

        id:5    name:dagou      score:60        remark:hello
        id:3    name:xiaoming   score:65        remark:hello
        id:4    name:ergou      score:80        remark:hello
        id:1    name:lili       score:85        remark:hello
        id:2    name:liming     score:90        remark:hello

-- sort by student id after replace ergou:

        id:1    name:lili       score:85        remark:hello
        id:2    name:liming     score:90        remark:hello
        id:3    name:xiaoming   score:65        remark:hello
        id:4    name:ergou      score:33        remark:modified by program
        id:5    name:dagou      score:60        remark:hello

-bash-4.2#

标签:容器,remark,name,hello,索引,score,student,boost,id
From: https://www.cnblogs.com/dujn/p/17705351.html

相关文章

  • MySQL存储过程、索引、分表对比
    MySQL存储过程、索引和分表是用于提高查询效率的三种不同方法,它们各自对查询效率有不同的影响和应用场景。以下是它们的对比:MySQL存储过程:影响查询效率:存储过程通常不直接影响查询效率,因为它们是用于封装查询逻辑和执行多个SQL语句的数据库对象。存储过程主要有助于减少网络......
  • 软件测试|深入解析Docker Run命令:创建和启动容器的完全指南
    简介Docker是一种流行的容器化平台,用于构建、分发和运行应用程序。其中一个最基本且重要的Docker命令是dockerrun,用于创建和启动容器。本文将详细解析dockerrun命令的用途、参数和示例,帮助您全面掌握创建和启动容器的过程。dockerrun在Docker中,容器是运行应用程序的独立环......
  • 软件测试|全面解析Docker Start/Stop/Restart命令:管理容器生命周期的必备工具
    简介Docker是一种流行的容器化平台,用于构建、分发和运行应用程序。在使用Docker时,经常需要管理容器的生命周期,包括启动、停止和重启容器。本文将详细介绍Docker中的dockerstart、dockerstop和dockerrestart命令,帮助您全面了解如何管理容器的运行状态。在Docker中,容器是独立......
  • 软件测试|Docker cp命令详解:在Docker容器和主机之间复制文件/文件夹
    简介Docker是一种流行的容器化平台,它允许开发人员在独立、可移植的环境中构建、打包和部署应用程序。在使用Docker时,常常需要在Docker容器和主机之间进行文件的复制和共享。Docker提供了一个名为dockercp的命令,可以轻松地在容器和主机之间复制文件和目录。本文将详细介绍docker......
  • MySQL索引详解(Hash、AVL、红黑树、B、B+)
    MySQL索引详解|JavaGuide(Java面试+学习指南)索引介绍索引是一种用于快速查询和检索数据的数据结构,其本质可以看成是一种排序好的数据结构。索引的作用就相当于书的目录。打个比方:我们在查字典的时候,如果没有目录,那我们就只能一页一页的去找我们需要查的那个字,速度很慢。......
  • 在 Linux Docker 容器中安装 Windows 虚拟机
    1查看或开启虚拟化我们需要检查我们的系统是否支持虚拟化。由于我们的容器将运行虚拟机管理程序,因此除非主平台支持虚拟化,否则它将无法工作。物理机可以通过bios进行开启,虚拟机也需要开启。这个是虚拟化的内容,所以自行百度打开sudoegrep-c'(vmx|svm)'/proc/cpuinfo2安......
  • spring-ioc容器
    不需要自己创建IOC容器对象任何需要的bean都可以在测试类中直接享受自动装配首先导入依赖<!--junit5测试--><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.3.1</version></depend......
  • 点击滚动容器内元素,滚动条自动滚动,元素水平居中或者垂直居中
    话不多说,先来个完整的html例子,可以直接复制到一个html文件中看效果:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><......
  • Flutter插件flutter_boost 在android模块中的报红问题解决.
    1,在开发Flutter插件时,打开插件的android项目,准备编写native端的代码时,发现各种报红,代码无法跳转,体验十分不好。就像我下面的截图一样:导入了FlutterBoostflutterBoost源码爆红。但是运行正常。。这说明本身是没有问题的。。分明是没有错误的类都存在。但是就是爆红。。。。可......
  • 【MySQL优化】索引失效的场景
    目录索引失效场景未遵循最左前缀匹配导致索引失效索引列参与运算索引列使用了函数类型转换导致索引失效OR引起的索引失效负向查询导致索引失效索引字段使用ISNOTNULL导致失效两列数据做比较会导致索引失效索引失效场景假设存在如下表:CREATETABLE`t_employee`(`id`i......