Comparator for use with sorting

I looked at http://www.cplusplus.com/reference/algorithm/sort/ and wanted to implement something similar: I defined a clauseComparator function, which is similar to myfunc in the example in the link above.

 bool QueryEvaluatorPrivate::clauseComparator(QueryClause cl1, QueryClause cl2) { int priority1 = clausePriority(cl1), priority2 = clausePriority(cl2); return priority1 < priority2; } 

and I used it like:

 sort(clauses.begin(), clauses.end(), clauseComparator); 

But VS complains:

 Error 4 error C3867: 'QueryEvaluatorPrivate::clauseComparator': function call missing argument list; use '&QueryEvaluatorPrivate::clauseComparator' to create a pointer to member h:\dropbox\sch\cs3202\code\source\query\query evaluator\queryevaluator.cpp 138 Error 5 error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided h:\dropbox\sch\cs3202\code\source\query\query evaluator\queryevaluator.cpp 138 

First, what's wrong (list of missing teams)? I tried to execute the sentence indicated in the error, add & and as a result

 Error 4 error C2276: '&' : illegal operation on bound member function expression h:\dropbox\sch\cs3202\code\source\query\query evaluator\queryevaluator.cpp 138 Error 5 error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided h:\dropbox\sch\cs3202\code\source\query\query evaluator\queryevaluator.cpp 138 

What is wrong here? In my opinion, it should pass a pointer to a function, which, as I understand it, should also be accepted sort ?

+4
source share
4 answers

You need to make your member function static so that sort is available. In the header of your class, please declare it as such:

static bool QueryEvaluatorPrivate::clauseComparator(const QueryClause & cl1, const QueryClause & cl2);

There is no need to use bind , since your comparator most likely (or should not) have access to class members.

+5
source

You cannot use member functions for this. Make clauseComparator free function or member function static .

Your comparator should also accept arguments using the const reference.

+1
source

You must use std::bind to bind a member function to std::sort , because otherwise you cannot bind a member function to std::sort

so he would like to do this:

  sort(clauses.begin(), clauses.end(), std::bind(&QueryEvaluatorPrivate::clauseComparator, instance, args)); 
0
source

because std :: sort (this view, right?) is declared as

 template <class RandomAccessIterator> void sort (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); 

therefore, the comp function should be:

A binary function that takes two elements in a range as arguments and returns a value convertible to bool. The return value indicates whether the element passed as the first argument is considered to be the second in the specific strict weak order that it defines. a function must not change any of its arguments. It can be a function pointer or a function object.

so please make a global function

 bool myfunction (QueryClause q1,QueryClause q2) { return (//condition); } 

or declare a functor:

 struct myclass { bool operator() (QueryClause q1,QueryClause q2) { return (//condition again);} } myobjectcomparator; 

and you need:

 QueryClause(const QueryClause &qc) { } 

to guarantee stability

0
source

Source: https://habr.com/ru/post/1468966/


All Articles