问题标签 [boost-range]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
546 浏览

c++ - c++ boost range adapter 多个向量

当我必须对向量执行转换/函数时,我最近才开始在 boost 中使用范围适配器。下面是我开始使用范围适配器时遇到的最简单示例之一的片段。

如果我的函数需要两个参数而不是一个参数怎么办,有什么方法可以将两个向量传递给范围适配器?例如,在这种情况下:

我仍然可以通过范围适配器将两个向量num1和我的函数传输到我的函数中吗?num2也许是这样的:

0 投票
2 回答
73 浏览

c++ - 算法在容器中找到一个元素,其成员之一具有给定值

我经常要做的事情是在元素集合中找到一个成员,该成员具有给定值的元素。例如给出:

我想找到名为“Alice”的人。一种方法是(使用升压范围适配器):

对于如此简单的操作,这是很多样板。难道不应该做类似的事情:

(无法编译,因为 &Person::getName 不是一元函数)

有没有一种简单的方法可以为成员获取一元函数?

0 投票
2 回答
129 浏览

c++ - 提升范围可以转换范围内的相邻元素吗?

如果我有一个范围并且我想转换相邻的对,是否有一个提升范围适配器可以做到这一点?

例如

输出将是

编辑

我尝试使用范围适配器

但是我不知道如何推断|的返回类型 操作员。如果我能做到这一点,那么适配器几乎解决了。

0 投票
1 回答
55 浏览

c++ - 如何创建具有用于捕获上下文的额外参数的 boost::range::transform 版本

例如我有一个向量

和一个结构

我希望做以下事情

但是,以上内容不能在我需要使用的 boost(1.55) 版本和编译器版本(VS-2010) 中编译。问题是当 lambda 通过引用捕获变量时,转换适配器最终会尝试使用非法的分配构造函数并且无法编译。但是,如果没有捕获任何内容,则一切正常。

我天真的解决方案是用另一个重载包装变换,这样

这似乎是 std 库首选的模式。例如std::lower_bound使用这种模式。

但是一旦我尝试考虑如何做到这一点,我就陷入了试图包装原始变换函数的模板疯狂中。如果有人可以向我展示如何生成我需要的重载,我相信我可以将其推断为我需要的其他重载。

另一种解决方案不是使用 lambda,而是为每个函数使用完整的函子,但如果我至少可以使用非捕获 lambda,这很丑陋。

0 投票
1 回答
194 浏览

c++ - 如何使用 boost-range 在函数中封装自定义迭代器

最近我boost-range用来创建满足某些标准的元素范围。在所有情况下,我一直使用相同类型的过滤范围,因此我尝试将此行为封装在外部函数中。

这就是我的问题开始的地方。考虑以下示例。

第一个 for 循环的行为与预期的一样,打印 4 和 8。然而,第二个 for 循环只打印 4。这是为什么呢?

我的第二个想法是实现一个具有begin()andend()功能的类。这应该是范围对象周围的薄包装。

在弄清范围迭代器的类型之后,这就是解决方案。

用法应该是这样的:

不幸的是,它只打印了 4。这对我来说很奇怪?

现在,我自己找到了解决方案。我必须删除我的 lambda 函数中的“&”才能使其工作!

0 投票
1 回答
495 浏览

c++ - 我编写了 C++ 代码来将 boost::optional 视为一个范围,但它不能编译

我希望将 boost::optional 视为可以包含零个或一个元素的容器。从逻辑上讲,我应该能够为容器创建一个迭代器并在其上使用 boost::for_each。我的尝试如下,但无法编译。我在GodBolt上试过了。

https://godbolt.org/g/z5MFfe

下面的代码供参考。

我相当确定我已经正确地实现了范围和迭代器的概念,但是我在 gcc 8.1 中得到了以下编译器错误(根据 godbolt)

0 投票
0 回答
212 浏览

c++ - 使用 boost::range 的相邻适配器

我在问自己是否可以boost-range通过适配器进行扩展,我称之为adjacentAdaptor. 这个适配器基本上应该遍历 a 中的所有相邻元素对vectorlist依此类推。

我认为这个函数在我的用例中非常有用,我经常需要遍历表示时间步长的列表。

最后一个 for 循环的输出应该是这样的:

只有一个元素或没有元素的向量不应该产生任何结果。

我尝试使用boost::adaptors::sliced生成必要的子列表,但后来我不知道如何boost::range帮助我将两个子范围压缩到一个。

我刚刚找到了一个可能的解决方案boost::iterators,但我真的不喜欢必须编写的代码量。我也错过了firstsecond而是我不得不写一个笨拙的get<>. 不幸的是,如果向量为空,程序就会崩溃!

我很高兴能在这个问题上得到任何帮助。

自己的部分解决方案

经过一些模板类型的推断,我想出了一些相对有用的东西。

first不过,使用and访问元素会更好second,但我不知道如何实现这种行为。

0 投票
1 回答
166 浏览

c++ - 一起使用 boost compute 和 boost::range 时,如何避免 boost::compute::zip_iterator 和 boost::iterators::zip_iterator 冲突?

我想boost::compute and boost::range一起使用,如下所示,但如果我取消注释该#include <boost/range/combine.hpp>行,我会收到一条错误消息,boost::compute::zip_iterator并且boost::iterators::zip_iterator是模棱两可的。有没有办法解决这个问题,以便我可以在同一个 cpp 文件中使用boost::compute和一起使用?boost::range我在 Windows vs2015 64bit 上使用。提升 1.67.0。

更新:我发现如果我尝试包含boost/iterator/zip_iterator.hpp而不是会发生相同的错误boost/range/combine.hpp,因此它不是特定于 Boost Range 库。

代码:

错误:

1>----- 构建开始:项目:demo,配置:Debug x64 ------ 1> demo.cpp 1> 这个头文件是实现细节并提供向后兼容性。1>C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(67): error C2668: 'boost::compute::make_zip_iterator': 对重载函数的模糊调用 1> C:\local\boost_1_67_0\boost /compute/iterator/zip_iterator.hpp(276): 注意: 可能是'boost::compute::zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost ::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>> boost: :compute::make_zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::
T=boost::compute::float4_, 1>
IteratorTuple=boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples ::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type> 1> ] 1> C :\local\boost_1_67_0\boost/iterator/zip_iterator.hpp(357): 注意: 或
'boost::iterators::zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost ::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>> boost::iterators::make_zip_iterator,boost::compute::buffer_iterator,boost: :tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples ::null_type,boost::tuples::null_type>>(IteratorTuple)' [使用参数相关查找找到] 1> with 1> [ 1> T=boost::compute::float4_, 1>
IteratorTuple=boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type, boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type> 1> ] 1> C:\local\boost_1_67_0\boost/compute/algorithm/ transform.hpp(67): 注意:在尝试匹配参数列表时 '(boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type, boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>)' 1> 与 1> [ 1>
T=boost::compute::float4_ 1> ] 1> C:\workspaces\compute_test\src\demo.cpp(45): 注意:见参考函数模板实例化'OutputIterator boost::compute::transform,boost: :compute::buffer_iterator,boost::compute::buffer_iterator,boost::compute::lambda::expression>(InputIterator1,InputIterator1,InputIterator2,OutputIterator,BinaryOperator,boost::compute::command_queue &)' 正在编译 1> 1> [ 1>
OutputIterator=boost::compute::buffer_iterator, 1>
T=boost::compute::float4_, 1>
Expr=boost::proto::exprns_::basic_expr,0>>,const boost: :compute::lambda::expression>,0>> &,const boost::compute::lambda::expression>,0>> &>,3>, 1>
InputIterator1=boost::compute::缓冲区迭代器,1>
InputIterator2=boost::compute::buffer_iterator, 1>
BinaryOperator=boost::compute::lambda::expression,0>>,const boost::compute::lambda::expression>,0>> &,const boost::compute::lambda::expression>,0> > &>,3>> 1> ] 1>C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(68): error C2668: 'boost::compute::make_zip_iterator': 对重载函数的模糊调用1> C:\local\boost_1_67_0\boost/compute/iterator/zip_iterator.hpp(276): 注意: 可能是'boost::compute::zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type ,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost ::tuples::null_type>> boost::compute::make_zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost: :tuples::null_type>>(IteratorTuple)' 1> with 1> [ 1>
T=boost::compute::float4_, 1>
IteratorTuple=boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples ::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type> 1> ] 1> C :\local\boost_1_67_0\boost/iterator/zip_iterator.hpp(357): 注意: 或
'boost::iterators::zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost ::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>> boost::iterators::make_zip_iterator,boost::compute::buffer_iterator,boost: :tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples ::null_type,boost::tuples::null_type>>(IteratorTuple)' [使用参数相关查找找到] 1> with 1> [ 1> T=boost::compute::float4_, 1>
IteratorTuple=boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type, boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type> 1> ] 1> C:\local\boost_1_67_0\boost/compute/algorithm/ transform.hpp(68): 注意:在尝试匹配参数列表时 '(boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type, boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>)' 1> 与 1> [ 1>
T=boost::compute::float4_ 1> ] 1>C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(72): error C2672: 'transform': 找不到匹配的重载函数 1>C: \local\boost_1_67_0\boost/compute/algorithm/transform.hpp(72): error C2780: 'OutputIterator boost::compute::transform(InputIterator1,InputIterator1,InputIterator2,OutputIterator,BinaryOperator,boost::compute::command_queue &) ': 需要 6 个参数 - 提供 4 个 1> C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(55): 注意:参见 'boost::compute::transform' 的声明 ====== ==== 构建:0 成功,

1 失败,2 最新,0 跳过 ==========

0 投票
2 回答
266 浏览

c++ - 从转换后的范围构造新容器

在我的代码中,我经常需要从之前转换的范围创建一个新容器。到目前为止,我已经使用boost::adaptors::transformed和的组合boost::copy_range来完成这项工作,认为容器的构造函数应该能够预先分配必要的内存。不幸的是,我注意到它boost::adaptors::transform返回一个 SinglePassRange 并且我不确定范围的大小是否可以在恒定时间内确定。

所以我的问题是:理想情况下,从转换范围构造一个新容器的最佳通用方法是一个表达式?

0 投票
2 回答
377 浏览

c++ - c++ 提升范围。数组中的对数组

我有一个 C 整数数组及其大小,即int* arr, unsigned size. 我想从中获得类似的视图,它将具有成对的整数作为元素。

澄清一下,任务是:我收到一个类似的数组[1,2,3,4],我想要一个视图,这将是 smth like [(1,2),(3,4)]

是否有任何便捷的方法可以通过 boost 或 std 范围以这种方式转换数组?