2016-08-19 6 views
0

Я пытаюсь найти индекс/расположение итератора, я использую thrust :: distance(). однако он возвращает странное значение. размер вектора равен 10. И когда я использую этот метод, он возвращает значение «131». - это полностью рабочий пример.Удерживающее устройство Векторное расположение итератора

#include <thrust/device_vector.h> 
#include <thrust/host_vector.h> 
#include <thrust/reduce.h> 
#include <thrust/extrema.h> 
#include <iostream> 
#include <iomanip> 
#include <thrust/sort.h> 
#include <thrust/copy.h> 
#include <thrust/random.h> 
#include <thrust/unique.h> 
#include <thrust/reduce.h> 
#include <thrust/iterator/constant_iterator.h> 

using namespace std; 
template <typename Vector> 
void print_vector(const std::string& name, const Vector& v) 
{ 
    typedef typename Vector::value_type T; 
    std::cout << " " << std::setw(20) << name << " "; 
    thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, "")); 
    std::cout << std::endl; 
} 

int main() 
{ 
thrust::device_vector<int> x; 
x.push_back(1); 
x.push_back(10); 
x.push_back(1); 
x.push_back(11); 
x.push_back(1); 
x.push_back(11); 
thrust::device_vector<int> y(10); 

print_vector("Original",x); 

thrust::sort(x.begin(),x.end()); 
print_vector("sort",x); 

thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end()); 
std::cout<<*it<<std::endl; 

//int newsize=it-y.begin(); 
int newsize=thrust::distance(y.begin(),it); 
cout<<"nsz:"<<newsize<<endl; 

return 0; 
} 

ответ

0

Итератор it устанавливается по отношению к вектору x:

thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end()); 
                ^  ^

Но вы просите расстояния от этого итератора в начале вектора y:

int newsize=thrust::distance(y.begin(),it); 
          ^

Это не имеет смысла. Между it и вектором y нет определенного отношения.

Если вы запрашиваете расстояния до начала вектора x вместо этого, вы получите более осмысленные результаты:

$ cat t1244.cu 
#include <thrust/device_vector.h> 
#include <thrust/host_vector.h> 
#include <thrust/reduce.h> 
#include <thrust/extrema.h> 
#include <iostream> 
#include <iomanip> 
#include <thrust/sort.h> 
#include <thrust/copy.h> 
#include <thrust/random.h> 
#include <thrust/unique.h> 
#include <thrust/reduce.h> 
#include <thrust/iterator/constant_iterator.h> 

using namespace std; 
template <typename Vector> 
void print_vector(const std::string& name, const Vector& v) 
{ 
    typedef typename Vector::value_type T; 
    std::cout << " " << std::setw(20) << name << " "; 
    thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " ")); 
    std::cout << std::endl; 
} 

int main() 
{ 
thrust::device_vector<int> x; 
x.push_back(1); 
x.push_back(10); 
x.push_back(1); 
x.push_back(11); 
x.push_back(1); 
x.push_back(11); 
thrust::device_vector<int> y(10); 

print_vector("Original",x); 

thrust::sort(x.begin(),x.end()); 
print_vector("sort",x); 

thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end()); 
std::cout<<*it<<std::endl; 

//int newsize=it-y.begin(); 
int newsize=thrust::distance(x.begin(),it); 
cout<<"nsz:"<<newsize<<endl; 

return 0; 
} 
$ nvcc -o t1244 t1244.cu 
$ ./t1244 
       Original 1 10 1 11 1 11 
        sort 1 1 1 10 11 11 
10 
nsz:3 
$ 
+0

Спасибо большое. ваш ответ показывает мне неправильное :) это большая ошибка для меня –