sorting - Sort by a function that takes argument(s) in c++? -
i'm trying write function sort vector of custom class objects variety of different attributes.
the c++ sort reference, found here:
http://www.cplusplus.com/reference/algorithm/sort/
says can sort this:
std::sort (myvector.begin(), myvector.end(), myfunction);
what able pass argument myfunction in add-on 2 objects vector this:
std::sort (myvector.begin(), myvector.end(), myfunction(mode=7));
do know of way so?
i relatively new c++, coming python easy.
you can utilize functor instead of free function:
struct functor{ int mode; bool operator() (int a,int b) { homecoming (a<b);} } functor;
the overloaded ()
operator executes when functor called sort
. in there can have variable mode
, utilize need. set mode (you set in on functor constructor) , phone call sort
using it:
functor.mode = 7; // or set in constructor std::sort (myvector.begin(), myvector.end(), functor);
c++ sorting
No comments:
Post a Comment