I'm trying to sort a vector using another vector as the sort key. I think I know what I need to do, but I was wondering if someone could tell me if I'm on the right track.
So, both vectors are members of a class. i.e.,
1 2 3 4 5
class Foo {
private:
vector<int> data;
vector<int> key;
};
I fed this to the <algorithm> sort function:
sort (data.begin(), data.end(), MySortFunction);
which didn't work since key is a private member. I then made MySortFunction as a member of class Foo, and that gave me compiling errors. I guess the function in the last argument cannot be a class method?
I looked around the Internet and it sounds like I have to use a functor. Define a class like:
Does this look correct? One thing I'm wondering is if this means SortByKey is instantiated every time I compare two items? That sounds like a lot of objects being created and destroyed -- so I was wondering if there is another way that I've completely missed...
In your 'Foo' class you can overload the booloperator<(const Foo &f) const then you can call std::sort(data.begin(), data.end());. So you don't need a compare function and you can access the private members
Thank you both of you for your suggestions! It looks like either solution will work for me and it seems like the use of a functor was a bit overkill. The pages that described it obviously didn't have the exact same problem that I had. And after I saw that page, I was stuck on functors and have been on that, thinking that was my only solution.
Thank you again; I'll use one of the two suggestions!
Thank you, firedraco! I actually don't understand functors well and have been reading up on it the last few days. I didn't know such a function existed; I'll look into it in case I need to use a functor in the future -- thanks!