I searched a lot and I am not sure if this is query is repeated but I used this as an reference to create a sort for my std::vector
which takes data of following type.
typedef struct {
int size;
int type;
int id;
} AC;
I was able to write individual function objects for each criteria.
However, my project requirement says that I need to have only once class or struct to include all the function objects for sorting according to size
,type
and id
. Is it possible to have all the function objects for std::sort
in one class?
I mean something like
Comparator com;
sort(iVec.begin(),iVec.end(),com.sortbysize);
sort(iVec.begin(),iVec.end(),com.sortbytype);
sort(iVec.begin(),iVec.end(),com.sortbyid);
I also looked at binary_functions
to fulfill my requirement but I was getting errors, when I declared more than one function object in a single class.
Further, is it necessary that function objects for std::sort
(and for that matter any STL algorithm which involves comparison) need to be bool operator()
or they can be normal functions returning bool
?
Yes to both: