Boost之multi_index

Boost.MultiIndex是一个功能强大的库,它允许创建的容器支持多个接口。以下是其功能介绍:

  • 多接口支持:Boost.MultiIndex的容器可以同时支持多个接口,这意味着可以从不同的角度和方式来访问和操作同一数据集。这种灵活性使得数据结构更加高效和易于管理。
  • 丰富的功能:除了基本的索引功能,Boost.MultiIndex还提供了额外的功能,如子对象搜索、范围查询、元素的就地更新和秩的计算。这些功能使得它可以方便地替代标准库中的std::set和std::multiset,即使不需要多索引功能。
  • 底层结构:Boost.MultiIndex的底层结构是用来存放结构体的map数据结构,但它不同于STL中普通的map,它允许用户根据需要指定索引的类型,即key的值可以根据不同的需求而变化。
  • 性能考虑:Boost.MultiIndex在设计时考虑了性能,确保即使在使用多个接口时也能保持高效的数据访问和操作。
  • 与其他库的协同:Boost.MultiIndex与其他Boost库协同工作,例如Boost.Bimap就是基于Boost.MultiIndex实现的,提供了可以从两侧查找元素的容器。

示例代码:

        

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <cstdint>
using namespace std;
using namespace boost::multi_index;


struct Person{
    uint64_t m_u64Id;       //ID
    string m_strName;       //姓名
    uint16_t  m_u16Age;     //年龄
    string m_strAddress;    //地址
    string m_strTel;        //电话号码

    friend ostream& operator<<(ostream& os, const Person& person)
    {
        os << person.m_u64Id << ", " << person.m_strName << ", " << person.m_u16Age << ", " << person.m_strAddress << ",   " << person.m_strTel;
        return os;
    }

    string name_age_address()const{
        return m_strName + to_string(m_u16Age) + m_strAddress;
    }
}; 

using Person_Set = multi_index_container<
    Person,
    indexed_by<
        ordered_unique<
            BOOST_MULTI_INDEX_MEMBER(Person, uint64_t, m_u64Id)>,  //ID是唯一的
        ordered_non_unique<
            BOOST_MULTI_INDEX_MEMBER(Person, string, m_strName)>,  //姓名不唯一
        ordered_non_unique<
            BOOST_MULTI_INDEX_MEMBER(Person, uint16_t, m_u16Age)>, //年龄不唯一
        ordered_non_unique<
            BOOST_MULTI_INDEX_MEMBER(Person, string, m_strAddress)>, //地址不唯一
        ordered_unique< 
            BOOST_MULTI_INDEX_MEMBER(Person, string, m_strTel)>,       //号码认为是唯一的
        ordered_unique<
            const_mem_fun<Person, string, &Person::name_age_address>>   //类似联合索引,这个两个索引可以唯一或不唯一
    >
>;

int main()
{
    Person_Set s;
    s.insert(Person{1, "小明", 22, "北京", "131 0000 00001"});
    s.insert(Person{2, "张三", 15, "上海", "131 0000 00002"});
    s.insert(Person{3, "李四", 19, "深圳", "131 0000 00003"});
    s.insert(Person{4, "王五", 11, "广州", "131 0000 00004"});
    s.insert(Person{5, "小黄", 12, "重庆", "131 0000 00005"});
    s.insert(Person{8, "小程", 18, "南京", "131 0000 00006"});

    Person_Set::nth_index<0>::type& index1 = s.get<0>();
    cout << "根据ID排序:" << endl;
    for(auto& item : index1)
        cout << item << endl;
    cout << endl;

    Person_Set::nth_index<1>::type& index2 = s.get<1>();
    cout << "根据名字排序:" << endl;
    for(auto& item : index2)
        cout << item << endl;
    cout << endl;
    
    Person_Set::nth_index<2>::type& index3 = s.get<2>();
    cout << "根据年龄排序:" << endl;
    for(auto& item : index3)
        cout << item << endl;
    cout << endl;

    Person_Set::nth_index<3>::type& index4 = s.get<3>();
    cout << "根据地址排序:" << endl;
    for(auto& item : index4)
        cout << item << endl;
    cout << endl;

    Person_Set::nth_index<4>::type& index5 = s.get<4>();
    cout << "根据电话号码排序:" << endl;
    for(auto& item : index5)
        cout << item << endl;
    cout << endl;
    return 0;
}

输出:

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值