zoukankan      html  css  js  c++  java
  • Boost练习程序(multi_index_container)

    代码来自:http://blog.csdn.net/whuqin/article/details/8482547

    该容器能实现多列索引,挺好。

    #include <string>
    #include <iostream>
    #include <boost/multi_index_container.hpp>
    #include <boost/multi_index/member.hpp>
    #include <boost/multi_index/ordered_index.hpp>
    
    using namespace boost;
    using namespace boost::multi_index;
    using namespace std;
    struct Employee{
        int id;
        string name;
        int age;
    
        Employee(int id_,string name_,int age_):id(id_),name(name_),age(age_){}
    
        friend ostream& operator<<(ostream& os,const Employee& e)
        {
            os<<e.id<<" "<<e.name<<" "<<e.age<<endl;
            return os;
        }
    };
    
    typedef multi_index_container<
    Employee,
    indexed_by<
    ordered_unique<member<Employee, int, &Employee::id> >,
    ordered_non_unique<member<Employee, string, &Employee::name> >,
    ordered_non_unique<member<Employee, int, &Employee::age> >
    >
    > EmployeeContainer;
    
    typedef EmployeeContainer::nth_index<0>::type IdIndex;
    typedef EmployeeContainer::nth_index<1>::type NameIndex;
    typedef EmployeeContainer::nth_index<2>::type AgeIndex;
    
    int main(){
        EmployeeContainer con;
        con.insert(Employee(0,"Joe",31));
        con.insert(Employee(1,"Robert",27));
        con.insert(Employee(2,"John",40));
    
        IdIndex& ids = con.get<0>();
        copy(ids.begin(),ids.end(), ostream_iterator<Employee>(cout));
        cout << endl;
    
        NameIndex& names = con.get<1>();
        copy(names.begin(), names.end(), ostream_iterator<Employee>(cout));
        cout << endl;
    
        //  names.erase(names.begin());
    
        AgeIndex& ages = con.get<2>();
        copy(ages.begin(), ages.end(), ostream_iterator<Employee>(cout));
        cout << endl;
    
        return 0;
    }
  • 相关阅读:
    2018-2019-20175307实验一《Java开发环境的熟悉》实验报告
    20175307《Java程序设计》第5周学习总结
    团队作业第二次——团队Github实战训练
    团队作业第一次—团队展示和项目展示
    第01组 团队Git现场编程实战
    2019 SDN上机第1次作业
    第01组 团队项目-需求分析报告
    团队项目-选题报告
    第二次结对编程作业
    第1组 团队展示
  • 原文地址:https://www.cnblogs.com/tiandsp/p/5014400.html
Copyright © 2011-2022 走看看