boost::multi_index 多索引容器

#include "stdafx.h"
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <iostream>
#include <string>
using namespace boost::multi_index;
// 定义数据结构
struct employee {
    int         id;
    std::string name;
    std::string department;
    double      salary;

    // 构造函数
    employee(int id, const std::string& name, const std::string& dept, double sal)
        : id(id), name(name), department(dept), salary(sal) {}

    // 便于打印
    friend std::ostream& operator<<(std::ostream& os, const employee& e) {
        return os << "[ID=" << e.id
                  << ", Name=" << e.name
                  << ", Dept=" << e.department
                  << ", Salary=" << e.salary << "]";
    }
};
// 定义多索引容器
typedef multi_index_container<
    employee,  // 存储的数据类型
    indexed_by<
        // 索引1: 按 ID 哈希查找,唯一
        hashed_unique<
            member<employee, int, &employee::id>
        >,

        // 索引2: 按姓名排序(可重复)
        ordered_non_unique<
            member<employee, std::string, &employee::name>
        >,

        // 索引3: 按部门哈希分组
        hashed_non_unique<
            member<employee, std::string, &employee::department>
        >,

        // 索引4: 按薪资降序排列
        ordered_non_unique<
            member<employee, double, &employee::salary>,
            std::greater<double>  // 降序
        >
    >
> employee_set;
int main() {
    employee_set employees;

    // 插入数据
    employees.insert(employee(1, "Alice", "Engineering", 90000));
    employees.insert(employee(2, "Bob",   "Engineering", 80000));
    employees.insert(employee(3, "Charlie", "Sales", 70000));
    employees.insert(employee(4, "David", "Sales", 75000));
    employees.insert(employee(5, "Alice", "HR", 60000));  // 同名不同部门

    std::cout << "所有员工(按薪资降序):\n";
    const auto& salary_index = employees.get<3>();  // 获取第4个索引(薪资)
    for (const auto& e : salary_index) {
        std::cout << e << "\n";
    }

    std::cout << "\n按姓名查找 'Alice':\n";
    const auto& name_index = employees.get<1>();  // 第2个索引:姓名
    auto range = name_index.equal_range("Alice");
    for (auto it = range.first; it != range.second; ++it) {
        std::cout << *it << "\n";
    }

    std::cout << "\n工程部员工:\n";
    const auto& dept_index = employees.get<2>();  // 第3个索引:部门
    auto eng_range = dept_index.equal_range("Engineering");
    for (auto it = eng_range.first; it != eng_range.second; ++it) {
        std::cout << *it << "\n";
    }

    std::cout << "\n查找 ID=2 的员工:\n";
    const auto& id_index = employees.get<0>();  // 第1个索引:ID(哈希唯一)
    auto it = id_index.find(2);
    if (it != id_index.end()) {
        std::cout << "Found: " << *it << "\n";
    } else {
        std::cout << "Not found.\n";
    }

    std::cout << "\n删除 ID=3 的员工 (Charlie)\n";
    id_index.erase(it);  // 可以通过任意索引删除

    std::cout << "剩余员工总数: " << employees.size() << "\n";

    return 0;
}
posted @ 2018-10-21 16:35  osbreak  阅读(1435)  评论(0)    收藏  举报