make_transform_iterator 作为 thrust::reduce 算法中的第二个参数有什么意义?
What is the point of the make_transform_iterator as the second parameter in thrust::reduce algorithm?
代码如下:
thrust::reduce(thrust::make_transform_iterator(v.begin(), square()),
thrust::make_transform_iterator(v.end(), square()));
摘自 transform_iterator 示例 here。具体来说,使用以下作为第二个迭代器不是多余的吗?
thrust::make_transform_iterator(v.end(), square())
为什么不直接使用以下内容呢?
thrust::reduce(thrust::make_transform_iterator(v.begin(), square()),
v.end());
我想在这个特定的示例中,由于 thrust::make_transform_iterator(v.begin(), square())
不会生成与 v
大小不同的迭代器,因此我更新后的代码将执行与原始代码相同的操作,是否正确?
编辑
我唯一的猜测是确保两个迭代器的类型相同?我发现证实我对此的怀疑是该文档中的以下文本 "the transform functor (namely square_root and square) inherits from thrust::unary_function. Inheriting from thrust::unary_function ensures that a functor is a valid AdaptableUnaryFunction and provides all the necessary typedef declarations." 虽然,我认为这具体指的是仿函数本身。
更新
请参阅 Robert Crovella 的评论以获取答案。
这里的迭代器用于标记特定序列的开始和结束。 v.end()
不标记序列的结束,这里的变换迭代器:
thrust::reduce(thrust::make_transform_iterator(v.begin(), square()),
v.end());
标志着开始。
这里的一些其他构造可能有更好的外观(美不胜收),例如:
auto my_iter = thrust::make_transform_iterator(v.begin(), square());
thrust::reduce(my_iter, my_iter+v.size());
代码如下:
thrust::reduce(thrust::make_transform_iterator(v.begin(), square()),
thrust::make_transform_iterator(v.end(), square()));
摘自 transform_iterator 示例 here。具体来说,使用以下作为第二个迭代器不是多余的吗?
thrust::make_transform_iterator(v.end(), square())
为什么不直接使用以下内容呢?
thrust::reduce(thrust::make_transform_iterator(v.begin(), square()),
v.end());
我想在这个特定的示例中,由于 thrust::make_transform_iterator(v.begin(), square())
不会生成与 v
大小不同的迭代器,因此我更新后的代码将执行与原始代码相同的操作,是否正确?
编辑
我唯一的猜测是确保两个迭代器的类型相同?我发现证实我对此的怀疑是该文档中的以下文本 "the transform functor (namely square_root and square) inherits from thrust::unary_function. Inheriting from thrust::unary_function ensures that a functor is a valid AdaptableUnaryFunction and provides all the necessary typedef declarations." 虽然,我认为这具体指的是仿函数本身。
更新
请参阅 Robert Crovella 的评论以获取答案。
这里的迭代器用于标记特定序列的开始和结束。 v.end()
不标记序列的结束,这里的变换迭代器:
thrust::reduce(thrust::make_transform_iterator(v.begin(), square()),
v.end());
标志着开始。
这里的一些其他构造可能有更好的外观(美不胜收),例如:
auto my_iter = thrust::make_transform_iterator(v.begin(), square());
thrust::reduce(my_iter, my_iter+v.size());