簡體   English   中英

功能對象無法正常工作

[英]Function object not working properly

我定義了以下函數對象:

struct Predicate1
{
   __device__ bool operator () 
       (const DereferencedIteratorTuple& lhs, const DereferencedIteratorTuple& rhs) 
  {
    using thrust::get;
    //if you do <=, returns last occurence of largest element. < returns first
    if (get<0>(lhs)== get<2>(lhs) && get<0>(lhs)!= 3) return get<1>(lhs) < get<1>(rhs); 
    else
    return true ;
  }
};

其中DereferencedIteratorTuple如下:

typedef thrust::tuple<int, float,int> DereferencedIteratorTuple;

而且,我稱其為:

result =  thrust::max_element(iter_begin, iter_end, Predicate1());

但是結果是元組(3,.99,4)。 我很困惑為什么會這樣,因為對於該元組,條件get<0>(lhs)== get<2>(lhs)不包含在if中。 因此,對於該元組的每次比較,運算符都返回true。 但是, thrust::max_element定義如下:

“此版本使用函數對象comp來比較對象。具體來說,此版本的max_element返回[first,last)中的第一個迭代器i,這樣,對於[first,last)中的每個迭代器j,comp(* i,* j)是假的。”

因此,對於這種元組,沒有辦法選擇它,運算符永遠不會返回false。 請讓我知道我做錯了

謂詞幫助算法確定首選元素。 如果謂詞返回true則算法優先使用rhs不是lhs 如果返回false則算法優先選擇lhs不是rhs 在謂詞始終返回true的情況下,算法將選擇數組中的最后一個元素。 對於stl和推力算法都是如此。

我猜想,由於rhs的第二個值小於0.99,因此在比較過程中以及每次未過濾時,您的結果都不會像lhs那樣發生。

如果要過濾此類值,則最好重寫謂詞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM