`

multi_index_container

 
阅读更多
根据不同的类中不同的字段排序

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

using namespace boost;
using namespace std;

struct Student
{
    Student(int i,string n,int a)
    {
        id = i;
        name = n;
        age = a;
    }
    friend std::ostream& operator<<(std::ostream& os,const Student& a)
    {
        os << "id:" << a.id << ",name:" << a.name << ",age:"<<a.age << std::endl;
        return os;
    }
    int id;
    string name;
    int age;
};

typedef boost::multi_index_container<Student,boost::multi_index::indexed_by<
        boost::multi_index::ordered_unique<boost::multi_index::member<Student,int,&Student::id>>,//id唯一
        boost::multi_index::ordered_unique<boost::multi_index::member<Student,string,&Student::name>>,
        boost::multi_index::ordered_non_unique<boost::multi_index::member<Student,int,&Student::age>>//age允许不唯一
        >
        > StudentContainer;

typedef StudentContainer::nth_index<0>::type IdIndex;
typedef StudentContainer::nth_index<1>::type NameIndex;
typedef StudentContainer::nth_index<2>::type AgeIndex;

int main()
{
    StudentContainer sc;
    sc.insert(Student(1,"zhangsan",20));
    sc.insert(Student(3,"lisi",22));
    sc.insert(Student(2,"wangwu",21));
    sc.insert(Student(4,"zhaoliu",22));

    IdIndex& sortById = sc.get<0>();
    NameIndex& sortByName = sc.get<1>();
    AgeIndex& sortByAge = sc.get<2>();

    cout << "sort by id:" << endl;
    copy(sortById.begin(),sortById.end(), ostream_iterator<Student>(cout));

    cout << "sort by name:" << endl;
    copy(sortByName.begin(),sortByName.end(), ostream_iterator<Student>(cout));

    cout << "sort by age:" << endl;
    copy(sortByAge.begin(),sortByAge.end(), ostream_iterator<Student>(cout));

}


sort by id:
id:1,name:zhangsan,age:20
id:2,name:wangwu,age:21
id:3,name:lisi,age:22
id:4,name:zhaoliu,age:22
sort by name:
id:3,name:lisi,age:22
id:2,name:wangwu,age:21
id:1,name:zhangsan,age:20
id:4,name:zhaoliu,age:22
sort by age:
id:1,name:zhangsan,age:20
id:2,name:wangwu,age:21
id:3,name:lisi,age:22
id:4,name:zhaoliu,age:22


account_object.hpp 320,371
分享到:
评论

相关推荐

    敏感词过滤

    BOOST multi_index_container Performance test condition: 1. Giving a sentence around 100 bytes (English & Chinese mixed) 2. Dirty phrases around 10,000 3. Do 1,000 loop test 4. Intel I7 ...

    层流:快速轻量的持续集成

    首先从您的发行版本的存储库或其他来源安装针对capnproto (version 0.7.0 or newer) , rapidjson , sqlite和boost (仅用于标头的multi_index_container库)的开发包。 在Debian Buster上,可以这样操作: sudo ...

    boost 1.41 中文文档,使用帮助,教程手册

    io state savers, iostreams, iterators, minmax, mpl, multi_array, multi_index, numeric/conversion, operators, optional, pointer container, pool, preprocessor, program_options, property map, property_...

    Boost_1_58_0.7z

    Asio, Chrono, Container, Context, Conversion, DateTime, Flyweight, Function, Functional/Factory, Fusion, Geometry, Hash, Interprocess, Intrusive, Lexical Cast, Log, Math, Move, Multi-index Containers...

    boost1.57源代码

    Updated Libraries: Any, Asio, Circular Buffer, Config, Container, Coroutine, Flyweight, Geometry, Interprocess, Intrusive, Iterator, Lexical Cast, Math, Move, MultiArray, Multiprecision, Multi-Index ...

    ViewPager 放大缩小左右移动

    * Position index of the first page currently being * displayed. Page position+1 will be visible if * positionOffset is nonzero. * @param positionOffset * Value from [0, 1) indicating ...

    THE BOOST C++ LIBRARIES

    13.4 Boost.MultiIndex 13.5 Boost.Bimap 13.6 Exercises Chapter 14: Data Structures 14.1 General 14.2 Boost.Tuple 14.3 Boost.Any 14.4 Boost.Variant 14.5 Exercises Chapter 15: Error Handling 15.1 General...

    微信小程序自定义多选事件的实现代码

    要实现下图的效果(自定义多选单选),大多数公司项目的多选框都是自己设计的,所以用原生标签或者组件是不可行的,最简单的是自己绑定事件,然后切换选择和未选择的图片。... /**利用数组的下标index来进行判

    python3.6.5参考手册 chm

    index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In Python 3.6 Summary – Release highlights New Features PEP 498: Formatted ...

    Visual C++ 编程资源大全(英文控件)

    11.zip Getting the number of columns in report view 获得列表视图的列数(2KB)&lt;END&gt;&lt;br&gt;3,12.zip 添加一列 Adding a column(2KB)&lt;END&gt;&lt;br&gt;4,13.zip Detecting column index of the item clicked...

    Google C++ International Standard.pdf

    4.7 Multi-threaded executions and data races . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 4.8 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    C++ 标准 ISO 14882-2011

    1.10 Multi-threaded executions and data races . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.11 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

Global site tag (gtag.js) - Google Analytics