multi_index_container 多索引容器

 

multi_index_container是c++ boost库中的一个多索引的容器。因工作中用到了,特来测试试用。

 1 #include "stdafx.h"
 2 #include "test.h"
 3 
 4 #include <string>
 5 #include <iostream>
 6 #include <boost/multi_index_container.hpp>
 7 #include <boost/multi_index/member.hpp>
 8 #include <boost/multi_index/ordered_index.hpp>
 9 
10 using namespace std;
11 using namespace boost;
12 using namespace boost::multi_index;
13 
14 struct Book{
15     int id;
16     int date;
17     string name;
18     string author;
19 
20     Book(int id_,
21          int date_,
22          string name_,
23          string author_)
24     {
25         id = id_;
26         date = date_;
27         name = name_;
28         author = author_;
29     }
30 };
31 
32 typedef multi_index_container<
33     Book,
34     indexed_by<
35     ordered_unique<member<Book, int, &Book::id> >,
36     ordered_non_unique<member<Book, int, &Book::date> >,
37     ordered_non_unique<member<Book, string, &Book::name> >,
38     ordered_non_unique<member<Book, string, &Book::author> >
39     > >BookContainer;
40 
41 typedef BookContainer::nth_index<0>::type Id_Index;
42 typedef BookContainer::nth_index<1>::type Date_Index;
43 typedef BookContainer::nth_index<2>::type Name_Index;
44 typedef BookContainer::nth_index<3>::type Author_Index;
45 
46 int _tmain(int argc, _TCHAR* argv[])
47 {
48     BookContainer con;
49     con.insert(Book(0, 2011, "math book", "jim"));
50     con.insert(Book(2, 2008, "chinese book", "jam"));
51     con.insert(Book(1, 2005, "english book", "roland"));
52     con.insert(Book(3, 2010, "music book", "rose"));
53 
54     Id_Index& id_idx = con.get<0>();
55     for (auto iter = id_idx.begin(); iter != id_idx.end(); iter++)
56     {
57         cout << iter->id << "  "
58             << iter->date << "  "
59             << iter->name << "  "
60             << iter->author << endl;
61     }
62 
63     cout << endl;
64 
65     Date_Index& date_idx = con.get<1>();
66     for (auto iter = date_idx.begin(); iter != date_idx.end(); iter++)
67     {
68         cout << iter->id << "  "
69             << iter->date << "  "
70             << iter->name << "  "
71             << iter->author << endl;
72     }
73 
74     cout << endl;
75 
76     Name_Index& name_idx = con.get<2>();
77     for (auto iter = name_idx.begin(); iter != name_idx.end(); iter++)
78     {
79         cout << iter->id << "  "
80             << iter->date << "  "
81             << iter->name << "  "
82             << iter->author << endl;
83     }
84 
85     cout << endl;
86 
87     Author_Index& author_idx = con.get<3>();
88     for (auto iter = author_idx.begin(); iter != author_idx.end(); iter++)
89     {
90         cout << iter->id << "  "
91             << iter->date << "  "
92             << iter->name << "  "
93             << iter->author << endl;
94     }
95 
96     getchar();
97     return 0;
98 }

输出:

 

可以看到以int型为索引的,输出是按照从小到大来排序的。以string为索引的,是按照字母顺序来输出的。

 

转载于:https://www.cnblogs.com/tyche116/p/9205362.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么还需要学习C++?】 你是否接触很多语言,但从来没有了解过编程语言的本质?你是否想成为一名资深开发人员,想开发别人做不了的高性能程序?你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹? 那么C++就是你个人能力提升,职业之路进阶的不二之选。【课程特色】 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。【学完后我将达到什么水平?】 1.对C++的各个知识能够熟练配置、开发、部署;2.吊打一切关于C++的笔试面试题;3.面向物联网的“嵌入式”和面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。【面向人群】 1.希望一站式快速入门的C++初学者; 2.希望快速学习 C++、掌握编程要义、修炼内功的开发者; 3.有志于挑战更高级的开发项目,成为资深开发的工程师。 【课程设计】 本课程包含3大模块基础篇本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。进阶篇本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接和命名空间等。提升篇:本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值