boost multi_index简单了解

#include <string>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/composite_key.hpp>

using namespace boost;
using namespace boost::multi_index;
using namespace std;

struct Employee{
    int id;
    string name;
    int age;

    Employee():id(0),age(0){}
    Employee(int id_,string name_,int age_):id(id_),name(name_),age(age_){}

    friend ostream& operator<<(ostream& os,const Employee& e)
    {
        os<<e.id<<"\t"<<e.name<<"\t"<<e.age<<endl;
        return os;
    }

    int get_id()const { return id; }
    const std::string& get_name()const { return name; }
    int get_age()const { return age; }
};

inline int get_student_age(const Employee &emp)
{
	return emp.age;
}

class ModifyEmployee
{
public:
    ModifyEmployee(int id_,string name_,int age_):id(id_),name(name_),age(age_){}
    void operator()(Employee &emp)
    {
        emp.id = id;
        emp.name = name;
        emp.age = age;
    }

private:
    int id;
    string name;
    int age;
};

struct emp_id{};
struct emp_name{};
struct emp_age{};
struct emp_name_age{};

typedef multi_index_container<
    Employee,
    indexed_by<

        ordered_non_unique< tag<emp_name_age>,  
                composite_key<
                    Employee,
                    member<Employee, string, &Employee::name>,
                    member<Employee, int, &Employee::age> > >,
        ordered_unique< tag<emp_id>, member<Employee, int, &Employee::id> >,
        ordered_non_unique< tag<emp_name>, member<Employee, string, &Employee::name> >,
        ordered_non_unique< tag<emp_age>, member<Employee, int, &Employee::age> >
    >
> EmployeeContainer;


//模板函数,用法: print_out_by<tagname>(multi_index_container_instance)
template <typename Tag, typename MultiIndexContainer>
void print_out_by(const MultiIndexContainer &s)
{
	/* obtain a reference to the index tagged by Tag */

	const typename boost::multi_index::index<MultiIndexContainer, Tag>::type &i = get<Tag>(s);

	typedef typename MultiIndexContainer::value_type value_type;

	/* dump the elements of the index to cout */

	std::copy(i.begin(), i.end(), std::ostream_iterator<value_type>(std::cout));
}

int main(){
    EmployeeContainer employee;
    employee.insert(Employee(1,"罗一",21));
	employee.insert(Employee(2,"周二",18));
	employee.get<emp_id>().insert(Employee(6,"郑六",21));
	employee.insert(Employee(7,"黄七",20));
	employee.insert(Employee(3,"张三",19));
	employee.insert(Employee(4,"李四",28));
	employee.insert(Employee(5,"李四",23));
	employee.insert(Employee(8,"王八",19));
	employee.insert(Employee(10,"杨十",22));

    //1 打印相关信息
	std::cout<<"Employee by ID"<<std::endl;
	print_out_by<emp_id>(employee);
	std::cout<<std::endl;

    std::cout<<"Employee by Name"<<std::endl;
	print_out_by<emp_name>(employee);
	std::cout<<std::endl;

    std::cout<<"Employee by Age"<<std::endl;
	print_out_by<emp_age>(employee);
	std::cout<<std::endl;

    std::cout <<"Employee by name&age" << std::endl;
	print_out_by<emp_name_age>(employee);

    return 0;
}
posted @ 2019-12-04 11:14  sfdevs  阅读(272)  评论(0)    收藏  举报