multi_index_container 多索引容器

本文通过实例演示了如何使用 Boost 库中的 multi_index_container 容器。该容器支持多种索引类型,例如唯一和非唯一有序索引。通过定义 Book 结构体并使用 multi_index_container 实现基于 id、日期、名称及作者的索引,展示了如何插入数据并按不同索引进行查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

Boost库中的multi_index是一种强大的工具,它允许你在STL容器(如map、set等)中创建多个索引视图,每个索引对应于不同的排序标准或访问模式。以下是使用boost::multi_index的基本步骤: 1. **包含头文件**: 首先,你需要包含`<boost/multi_index_container.hpp>`和需要的单个或多索引的头文件。 ```cpp #include <boost/multi_index_container.hpp> #include <boost/multi_index/member.hpp> ``` 2. **声明multi_index_container**: 定义一个multi_index_container,指定存储的数据类型(例如std::string),以及你想要创建的索引类型。你可以选择primary_index作为默认索引,也可以添加secondary_indices。 ```cpp using index_type = boost::multi_index::ordered_set<std::string>; typedef boost::multi_index_container< std::pair<std::string, int>, // 存储的数据类型 boost::multi_index::indexed_by< boost::multi_index::primary_index<>, boost::multi_index::hashed_non_unique<index_type> > >; ``` 这里`hashed_non_unique`表示元素是唯一的,但是查找速度较快(因为使用哈希表)。 3. **创建实例**: 实例化multi_index_container,并为其分配内存。 ```cpp index_type my_index; ``` 4. **插入数据**: 使用insert函数将数据加入容器,并关联到适当的索引。 ```cpp my_index.insert({"key1", value1}); ``` 5. **通过索引访问数据**: 可以通过索引来检索数据,如primary_index用于快速查找,而index_type可以用于按照自定义键值进行搜索。 ```cpp // Primary index search auto it = my_index.begin(); for (; it != my_index.end(); ++it) { std::cout << "Primary Index: " << it->first << ", Value: " << it->second << '\n'; } // Indexed by search auto search_result = my_index.find("key1"); if (search_result != my_index.end()) { std::cout << "Hashed Search: " << search_result->first << ", Value: " << search_result->second << '\n'; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值