문제

class StrangeFunctor 
{ 
  public: 
    StrangeFunctor(int (*comp)(string, string)) 
     { 
       this->comp = comp; 
     } 
     int operator()(string str1, string str2) 
     { 
       return –comp(str1, str2); 
     } 
  private: 
   int (*comp)(string, string); 
} 

I was just curious as what the above code actually did. Assuming the functor was properly initialized and given to a sorting function for comparison purpose, my hunch is that it reverses the order of the passed argument, but I'm not sure if that is correct and why the would be correct.

도움이 되었습니까?

해결책

This functor takes in a function pointer and then flips the sign on that method's return value.

return –comp(str1, str2); 

If used with sorting like you said, it would invert the order of what was being sorted, given by the original function pointer.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top