custom comparison function

Hi all,

I am trying to use the function std::sort in the following way:

1
2
3
4
template typename<T> void SomeCLass<T>::some_memberfunction(...) {
   vector<int> ind = ... // ( basically, integers from 1 to n, say)
   std::sort(ind.begin(), ind.end(), comparison_func);
}


with
1
2
3
4
template typename<T> bool SomeCLass<T>::comparison_func(int a, int b) 
{
   return ( (this->SomeMemberVector)[a] < (this->SomeMemberVector)[b]);
}


I don't understand why the compiler returns, after instanciation in int of previous class:
bool (SomeClass<int>::)(int, int)’ does not match ‘bool (SomeClass<int>::*)(int, int)’ .

If anyone of you could help on that, it would be really fantastic!
Cheers everyone.
JC

EDIT: I realize that I shouldn't have posted this message in that part of the forum, sorry about that.
I think it should be posted in 'General C++ Programming' section instead. I won't double-post though.

EDIT again:

I have used another way (which does work either ...), using functors (as found while browsing the forum) :

1
2
3
4
5
6
7
8
template <typename T> struct CmpPairs {
  CmpPairs(const vector<T> &v): v_(v) {}
  Vector<T> & v;
  bool operator()(int a, int b) const { return 'some comparison involving v'; };
};

template <typename T> CmpPairs<T> CreateCmpPairs(const vector<T>& v) { return CmpPairs<T>(v); };


then I would rewrite SomeCLass<T>::some_memberfunction as :
1
2
3
4
template typename<T> void SomeCLass<T>::some_memberfunction(...) {
   vector<int> ind = ... // ( basically, integers from 1 to n, say)
   std::sort(ind.begin(), ind.end(), CreateCmpPairs(this->SomeMemberVector));
}


This time, I have the following error:
 
error: there are no arguments to ‘CreateCmpPairs’ that depend on a template parameter, so a declaration of ‘CreateCmpPairs’ must be available.


I am so confused, because this is obviously wrong: the definition of 'CreateCmpPairs' involves 'vector<T> &v' as a template parameter! What did I do wrong ?
Last edited on
Your first problem is that std::sort cannot take non-static member functions natively.

Another problem is that struct CmpPairs' data member must be a const reference. But I don't even understand why struct CmpPair has a data member anyway.

A third problem is that operator() should take two T instances instead of ints.

A fourth problem is you have a couple of typos: template typename<T> -> template< typename T>. I suspect it was just a transcription error.
Hi jsmith,

thanks a lot for your reply. So if I am understand you well:

1) My first attempt using the comparison_func that is a member function of my class will not work, since I have to make it static and std::sort doesn't like it. Crap ...
2) and 3) Sorry I forgot to mention the purpose of this code: I want to sort a vector say in a natural order. But I also want to apply the corresponding permutation to other member vectors.
So I thought I would first sort a vector (1, ..., n) using a custom comparison function that compares the element of my initial vector. I would then generate a permutation vector, and reorder everything else.
For example, the comparison functor would take my reference vector (the one I want to actually sort) and generate the corresponding comparison function:

1
2
3
4
5
template <typename T> struct CmpPairs {
  CmpPairs(const vector<T> &v): v_(v) {}
  const Vector<T> & v; // I've added your correction.
  bool operator()(int a, int b) const { return 'some comparison involving v'; };
};


Then when I sort the vector<int> ind (as per my 1st post), then the comparison functor takes as requested two ints as inputs and returns a bool.
I still don't know how to implement this idea.

4) Sorry for the typo.

Thanks a lot for your help,
JCS
Last edited on
Hi jsmith,

I found the bug: I forgot a semi-column at the end of the constructor of CmpPairs ...
I can't believe I spent that much time on that.
Anyway, thanks for your great help.

JCS
Topic archived. No new replies allowed.