פתרון לשאלה של איך לעטוף פונקציות לאובייקט השוואה
#include <iostream>#include <set>
typedef struct {
int value;
} baz_t;
// some compare function we are bound to use
bool compare_func (const baz_t& _Left , const baz_t& _Right )
{
return _Left.value < _Right.value;
}
//template that can be used with the exported functions
template <class Arg1,class Arg2,class Result,Result (*f)(const Arg1 &,const Arg2 &) > struct compare_functor_by_function {
//The magic is by creating the () operator and calling the compare functions
Result operator() (const Arg1 & _Left,const Arg2 & _Right) {
return f(_Left,_Right);
}
};
int main()
{
std::set< baz_t, compare_functor_by_function<baz_t,baz_t,bool,compare_func> > obj;
baz_t useless_obj ;
baz_t useless_obj2;
useless_obj.value = 1;
useless_obj2.value = -1;
obj.insert(useless_obj);
std::cout << "next" << std::endl;
obj.insert(useless_obj2);
std::cout << "2" << std::endl;
obj.insert(useless_obj2);
return 0;
}
אין תגובות:
הוסף רשומת תגובה