按顺序搜索boost::multi_index,按顺序获取下一个元素
searching boost::multi_index by sequence and obtaining the next element by order
我想按顺序搜索boost::multi_index容器,并按顺序获得下一个元素。
下面的代码存储了四个具有不同索引(顺序和有序)的float。
最后一个if语句就是问题所在。我不知道如何编辑以按顺序获得下一个元素。
这里有一些代码:
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
using namespace boost::multi_index;
typedef multi_index_container <
float
, indexed_by<
sequenced<>
, ordered_non_unique<identity<float>>
>
> Floats;
int main() {
Floats floats;
auto & sequence=floats.get<0>();
auto & order=floats.get<1>();
order.insert(0.3);
sequence.push_back(0.1);
sequence.push_back(0.9);
order.insert(0.6);
// 0.1 0.3 0.6 0.9
for (auto i=order.begin(),j=order.end(); i!=j; ++i) {
std::cout << *i << std::endl;
}
// 0.3 0.1 0.9 0.6
for (auto i=sequence.begin(),j=sequenceend(); i!=j; ++i) {
std::cout << *i << std::endl;
}
auto i = order.find(0.3);
if (i!=order.end()) {
// get the next element by order, 0.6 in this case
}
}
auto j=++(floats.project<1>(i));
了解用于multi_index迭代器的project<N>
函数。文档在这里:
http://www.boost.org/doc/libs/1_59_0/libs/multi_index/doc/reference/multi_index_container.html#projection
所以你会写这样的东西:
auto iorder = project<0>(i);
相关文章:
- 为什么用户定义的函数不按照给定的顺序对相同长度的元素进行排序?
- 快速排序 - 三个中位数枢轴选择 - 某些元素顺序不正确
- 序列容器 - 只能按顺序访问元素
- 如何检查 2 个 c++ 数组在 O(1) 或 O(log n) 时间复杂度中是否相同(所有元素都相同,顺序很重要)?
- 保留从一个多集复制到另一个多集的元素的顺序
- 如何在 C++ 中将从文本文件中读取的元素推送和弹出到数组中,并按 Revserse 顺序输出堆栈?
- 查找数组的第一个和最后一个索引,其中 from 和 to 元素的顺序总和最大
- 编写一个递归功能,该功能采用数组并以相反顺序显示元素,而无需在末尾启动数组的索引
- 编写链接函数的更短/更有效的方法,该函数按字典顺序添加新元素
- stable_sort不遵守元素的顺序
- 从容器中获取随机元素,该容器在恒定时间内没有严格的元素顺序
- 如何在默认和二维数组中找到顺序扩大元素的位置
- 多重映射是否保证广告顺序与其初始值设定项中给出的元素顺序匹配
- 调用std::nth_element后第n个元素之前的元素顺序
- 控制地图中元素的顺序
- 指针集中元素的顺序
- 一种按排序顺序保持元素的数据结构,支持快速插入和计算连续元素之间的最大差异
- 双向循环列表中的赋值运算符以错误的顺序添加元素
- 更改STL多集中两个相等元素的顺序
- 初始值设定项列表中元素的求值顺序