How to use sort() in C++ with custom sort member function?
It is, but in general I would encourage just using a proper functor or a lambda:
Using a lambda:
std::sort(payloadIndices.begin(), payloadIndices.end(), [this](int a, int b){
return this->sortByWeights(a, b);
});
Alternatively using std::mem_fn
:
auto sorter = std::bind(std::mem_fn(SimpleGreedyMappingTechnique::sortByWeights), this);
std::sort(payloadIndices.begin(), payloadIndices.end(), sorter);
Alternatively using a functor:
namespace{
struct indicies_less_than
{
const SimpleGreedyMappingTechnique & mapping_tech;
indicies_less_than(const SimpleGreedyMappingTechnique & mapping_tech)
:mapping_tech(mapping_tech){}
bool operator()(int a, int b)
{
return mapping_tech.sortByWeights(a, b);
}
};
}
std::sort(payloadIndices.begin(), payloadIndices.end(), indicies_less_than(*this));
Note:
if the types being sorted were anything more complicated than an int
you would definitely want to pass them by const&
to prevent copying
Why custom comparator for set in c++ requires extra 'const' keyword
By adding that const
to the end of that member function you're making it a const member function, meaning that it cannot possibly modify any of your member variables. Because calling a non-const member function could modify the object, you can't call it if the object is const.
std::set
's erase member function requires that operator() be const to stop it from modifying the object's in their set without you realizing.
If any member function can be const then it should be const, just like any variables you declare or take in as arguments.
comparator in cpp sorting
The []
is the beginning of what is called a lambda
in C++.
You can read a nicely written answer on what lambdas are here :
What is a lambda expression in C++11?
Also, you haven't provided your original code so I can't point out the mistake. But from the error message, it seems that the function comp you created was inside a class. One way is to make the function global, after which you should be able to pass it comfortably. Another option is to make the function static (which i believe makes sense). You can then pass it sort like this: sort(a.begin(), a.end(), A::comp)
where A is class in which you made this function.
Getting non-standard syntax; use '&' to create a pointer to member for std::sort
custom_sort
is a non-static member function, it can't work with sort
directly because under the hood it expects three arguments, not two - the first one is the object pointer.
In your particular example, custom_sort
doesn't use any data members, so it can be made static
(static
should go into the declaration):
static bool LinearRegression::custom_sort(double a, double b);
Then you could write:
std::sort(error.begin(), error.end(), &custom_sort);
Note that you need &
to form a pointer to a member function, as the error message suggests. However, for static
member function it can be omitted (similar to free functions).
Custom comparator to segregate even and odd giving TLE
Your first comparator does not meet the requirements defined by Compare
since it doesn't take r
's value into account (i.e. it does not properly establish a "strict weak ordering").
For example, one requirement that the first comparator violates is:
For all
a
,comp(a,a)==false
Which we can easily break with custom_cmp(0, 0)
.
As an experiment I tried using the same comparator in Compiler Explorer, and interestingly it actually ends up invoking Undefined Behavior because of the unsatisfied contract.
Related Topics
Visual C++ Equivalent of Gcc'S _Attribute_ ((_Packed_))
Why Use Std::Bind Over Lambdas in C++14
Error Lnk2019: Unresolved External Symbol _Winmain@16 Referenced in Function _Tmaincrtstartup
How to Increase the Re-Usability of This Key-Oriented Access-Protection Pattern
Generating Combinations in C++
Why Do We Use Volatile Keyword
#Include All .Cpp Files into a Single Compilation Unit
Printing 1 to 1000 Without Loop or Conditionals
Why Do Constant Expressions Have an Exclusion For Undefined Behavior
Capturing Stdout from a System() Command Optimally
How Does Generic Lambda Work in C++14
How to Hide a String in Binary Code
What Are Inline Namespaces For
What Are the Correct Link Options to Use Std::Thread in Gcc Under Linux