使用transformer_iterator怎么样?这是一个简短的示例(按第一位排序),您可以根据自己的目的编写自己的一元函数。
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/sort.h>
using namespace std;
struct and_func : public thrust::unary_function<int,int>
{
__host__ __device__
int operator()(int x)
{
return 8&x;
}
};
int main()
{
thrust::device_vector<int> d_vec(4);
d_vec[0] = 10;
d_vec[1] = 8;
d_vec[2] = 12;
d_vec[3] = 1;
thrust::sort_by_key(thrust::make_transform_iterator(d_vec.begin(), and_func()),
thrust::make_transform_iterator(d_vec.end(), and_func()),
d_vec.begin());
for (int i = 0; i < 4; i++)
cout<<d_vec[i]<<" ";
cout<<"\n"<<endl;
return 0;
}