代码如下:
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
的迭代器,我更新后的代码会做与原始代码相同的事情,对吗?
编辑
我唯一的猜测是确保两个迭代器的类型相同?我发现证实我对此的怀疑的是该文档中的以下文本“转换仿函数(即 square_root 和 square)继承自 thrust::unary_function。继承自 thrust::unary_function 确保仿函数是有效的 AdaptableUnaryFunction 并提供所有必要的 typedef 声明。”虽然,我认为这具体指的是仿函数本身。
更新
请参阅 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());
关于c++ - make_transform_iterator 作为 thrust::reduce 算法中的第二个参数有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37883853/