sorting a vector of pointer to object

Hi! I have a problem writing my code and I did not find any solution yet..
I need to sort a vector of pointers, defined as vector<Function*>, using one of the parameters of the object. I tried to redefine the "<" operator for the Function object and then use it for the sort and it works. But my problem now is that I fill the vector with Function* in the main file and I'd like to do the sort there. But since I define the "<" operator or a struct Compare{} inside the Function.cc file I'm not able to use it in the main file..
so is there a way to use an operator defined inside a class for a sort in the main file?

I don't know if I managed to explain myself...but thanks anyway!

GIulia
Not sure what you mean, but it goes like this:

1
2
3
4
5
static bool ptr_Function_lt(Function* f1,Function* f2) {return *f1<*f2;}
[...]
vector<Function*> vec;
[...]
sort(vec.begin(),vec.end(),ptr_Function_lt);

Last edited on
Yes thanks I know it wasn't very clear...maybe because I'm a bit confused too.
Anyway...yes I already did something like that.
But does it still work if I define the static bool in a file (where the class is defined) and do the sort in another one(the file of the main program)?
If you want to use it in another translation unit, you just need to remove the static qualifier and declare the function in the other translation unit. Although it would be better to move the function into a header (in that case, don't forget that it needs to be static inline) or to create a comparator class instead.
Last edited on
Topic archived. No new replies allowed.