使用thrust::reverse
:
#include <thrust/device_vector.h>
#include <thrust/reverse.h>
#include <thrust/pair.h>
#include <iostream>
int main()
{
thrust::device_vector<thrust::pair<int,int> > vec;
vec.push_back(thrust::make_pair( 1,2));
vec.push_back(thrust::make_pair( 5,4));
vec.push_back(thrust::make_pair(-2,5));
vec.push_back(thrust::make_pair( 6,1));
vec.push_back(thrust::make_pair( 2,6));
std::cout << "input: " << std::endl;
for(int i = 0; i < vec.size(); ++i)
{
thrust::pair<int,int> x = vec[i];
std::cout << " [" << x.first << ", " << x.second << "]";
}
std::cout << std::endl;
thrust::reverse(vec.begin(), vec.end());
std::cout << "output: " << std::endl;
for(int i = 0; i < vec.size(); ++i)
{
thrust::pair<int,int> x = vec[i];
std::cout << " [" << x.first << ", " << x.second << "]";
}
std::cout << std::endl;
return 0;
}
输出:
$ nvcc reverse.cu -run
input:
[1, 2] [5, 4] [-2, 5] [6, 1] [2, 6]
output:
[2, 6] [6, 1] [-2, 5] [5, 4] [1, 2]