题
我目前正在尝试使用推力库,并且返回类型有问题。
device_vector<int> input(5);
input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;
pair<int*, int*> result= minmax_element(input.begin(), input.end());
给出错误:
error : no suitable conversion function from "const thrust::detail::normal_iterator<thrust::device_ptr<int>>" to "int *" exists
有人可以向我解释如何解决什么回报类型?
根据 文档 返回类型是
thrust::pair<ForwardIterator,ForwardIterator>
但是,这对我不起作用,有人可以解释一下!
谢谢!
解决方案
命名模板参数时, ForwardIterator
不命名任何特定类型。出于我们的目的,我们可以将其视为占有迭代器类型的占位符 thrust::minmax_element
:
template<typename ForwardIterator>
thrust::pair<ForwardIterator,ForwardIterator> minmax_element(ForwardIterator first, ForwardIterator last);
所以 minmax_element
返回a pair
将任何类型的迭代器作为参数赋予。
在您的情况下 minmax_element
返回a pair
的 device_vector<int>::iterator
s。您可以通过制作代码编译 result
适当的 pair
:
device_vector<int> input(5);
input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;
// we can use a typedef for brevity
typedef pair<device_vector<int>::iterator, device_vector<int>::iterator> result_type;
result_type result = minmax_element(input.begin(), input.end());
// check the results
assert(0 == *result.first);
assert(8 == *result.second);
不隶属于 StackOverflow