如何使equal_range迭代器在 Boost 多索引中按不同的索引排序?
How to make the equal_range iterator sorted by a different index in Boost Multi-index?
我在employee
类上有一个提升多索引容器(取自提升官方文档(:
typedef multi_index_container<
employee,
indexed_by<
ordered_unique<
tag<id>, BOOST_MULTI_INDEX_MEMBER(employee,int,id)>,
ordered_non_unique<
tag<name>,BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>,
ordered_non_unique<
tag<age>, BOOST_MULTI_INDEX_MEMBER(employee,int,age)> >
> employee_set;
下面是由 (id、name、age( 打印的容器中的数据示例:
0 Joe 31
1 Robert 27
2 John 40
3 Albert 20
4 John 57
5 John 58
6 John 22
我想有一个迭代器,其中包含名称为 John 的所有项目,按年龄排序(最后一个字段(。我尝试了equal_range
方法:
auto iter1 = boost::make_iterator_range(es.get<name>().equal_range("John"));
它返回一个迭代器,其中包含名为 John 的所有记录。如何使此迭代器按第三个索引(即年龄(排序?
输出应为:
6 John 22
2 John 40
4 John 57
5 John 58
好的。这是复制人Live On Coliru
输出确实是
2 "John" 40
4 "John" 57
5 "John" 58
6 "John" 22
现在,要按年龄排序(注意单词的选择(,您可以使用组合键。所以代替:
bmi::ordered_non_unique<
bmi::tag<struct name>,
bmi::member<employee, std::string, &employee::name>
>,
用
bmi::ordered_non_unique<
bmi::tag<struct name_age>,
bmi::composite_key<employee,
bmi::member<employee, std::string, &employee::name>,
bmi::member<employee, int, &employee::age>
>
>,
现在你可以
for (employee const& emp : boost::make_iterator_range(es.get<name_age>().equal_range("John"))) {
std::cout << emp.id << " " << std::quoted(emp.name) << " " << emp.age << "n";
}
指纹
6 "John" 22
2 "John" 40
4 "John" 57
5 "John" 58
完整示例
住在科里鲁
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/range/iterator_range.hpp>
namespace bmi = boost::multi_index;
struct employee {
int id;
std::string name;
int age;
};
typedef bmi::multi_index_container<
employee,
bmi::indexed_by<
bmi::ordered_unique<
bmi::tag<struct id>,
bmi::member<employee, int, &employee::id>
>,
bmi::ordered_non_unique<
bmi::tag<struct name>,
bmi::member<employee, std::string, &employee::name>
>,
bmi::ordered_non_unique<
bmi::tag<struct name_age>,
bmi::composite_key<employee,
bmi::member<employee, std::string, &employee::name>,
bmi::member<employee, int, &employee::age>
>
>,
bmi::ordered_non_unique<
bmi::tag<struct age>,
bmi::member<employee, int, &employee::age>
>
> > employee_set;
#include <iostream>
#include <iomanip>
int main() {
employee_set es {
{0, "Joe", 31},
{1, "Robert", 27},
{2, "John", 40},
{3, "Albert", 20},
{4, "John", 57},
{5, "John", 58},
{6, "John", 22},
};
std::cout << "name index:n";
for (employee const& emp : boost::make_iterator_range(es.get<name>().equal_range("John"))) {
std::cout << emp.id << " " << std::quoted(emp.name) << " " << emp.age << "n";
}
std::cout << "name_age index:n";
for (employee const& emp : boost::make_iterator_range(es.get<name_age>().equal_range("John"))) {
std::cout << emp.id << " " << std::quoted(emp.name) << " " << emp.age << "n";
}
}
指纹
name index:
2 "John" 40
4 "John" 57
5 "John" 58
6 "John" 22
name_age index:
6 "John" 22
2 "John" 40
4 "John" 57
5 "John" 58
相关文章:
- 如何根据排序索引的向量对 std::index 集进行排序?
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 排序谓词没有传递对索引对象的引用?
- 如何在对向量排序后更改索引值?c++
- 在不进行排序的情况下查找数组中n个最小值的索引
- 如何解决索引和排序问题
- 使用C++中的排序对索引进行排序
- 如何使equal_range迭代器在 Boost 多索引中按不同的索引排序?
- 为什么我的代码没有对数组中第二个索引上的数据进行排序?
- 根据某些条件对索引子集进行排序
- 如果我将索引变量更改为零,并且合并函数中的k = 0,则获得合并排序的错误结果
- 如何对同一数组索引下的结构成员进行排序?
- 如何在保留原始索引的同时对向量的向量进行排序
- 如何不对数组的元素进行排序,而是对数组的索引进行排序?
- C++ 将多索引提升为 LRU 缓存的索引排序问题
- C++ std::lower_bound() 函数来查找索引排序向量的插入点
- boost多索引排序索引中插入的匹配函数
- 使用变量索引排序向量的向量
- 使用索引排序,但保持索引顺序
- 比较c++中排序函数的索引排序