klopter
asked on
I want to use STL sort with a functor
//
// This compiles and runs if I comment out the
// call to sort().
// I would like to sort on key. But, I have no clue
// where to start on the syntax.
//
#include <vector>
#include <algorithm>
#include <iostream>
class keyPLUS {
public:
int key;
int something_else;
} ;
int main() {
vector<keyPLUS> KeyContainer(2);
KeyContainer[0].key = 5;
KeyContainer[0].something_ else = 13;
KeyContainer[1].key = 4;
KeyContainer[1].something_ else = 17;
sort( &KeyContainer[0], &KeyContainer[2],
what_do_I_put_here );
cout << " [0].key =" << KeyContainer[0].key
<< " [0].something_else =" << KeyContainer[0].something_ else << endl ;
cout << " [1].key =" << KeyContainer[1].key
<< " [1].something_else =" << KeyContainer[1].something_ else << endl ;
}
// This compiles and runs if I comment out the
// call to sort().
// I would like to sort on key. But, I have no clue
// where to start on the syntax.
//
#include <vector>
#include <algorithm>
#include <iostream>
class keyPLUS {
public:
int key;
int something_else;
} ;
int main() {
vector<keyPLUS> KeyContainer(2);
KeyContainer[0].key = 5;
KeyContainer[0].something_
KeyContainer[1].key = 4;
KeyContainer[1].something_
sort( &KeyContainer[0], &KeyContainer[2],
what_do_I_put_here );
cout << " [0].key =" << KeyContainer[0].key
<< " [0].something_else =" << KeyContainer[0].something_
cout << " [1].key =" << KeyContainer[1].key
<< " [1].something_else =" << KeyContainer[1].something_
}
> sort(KeyContainer.begin(), KeyContainer.end());
No, that will only work if you have a less than operator defined on the object, which you don't in this case. You have a couple of options:
i) define a < operator
class keyPLUS {
public:
int key;
int something_else;
bool operator<(const keyPlus& rhs)
{ return key < rhs.key; // or whatever it should be }
} ;
and then the sort call given above will work (you could also define a non-member function less than operator).
or
ii) define a functor, as you asked originally, the best way is to use a function object:
class keyPlusLessThan
{
bool operator()(const keyPlus& lhs, const keyPlus& rhs)
{ return lhs.key < rhs.key; // ? }
};
then the sort call becomes:
sort(keyContainer.begin(), keyContainer.end(), keyPlusLessThan());
you can also do it with a pointer to a function if you want, but the function object is probably better. Let me know if you want to see an example with a function pointer.
No, that will only work if you have a less than operator defined on the object, which you don't in this case. You have a couple of options:
i) define a < operator
class keyPLUS {
public:
int key;
int something_else;
bool operator<(const keyPlus& rhs)
{ return key < rhs.key; // or whatever it should be }
} ;
and then the sort call given above will work (you could also define a non-member function less than operator).
or
ii) define a functor, as you asked originally, the best way is to use a function object:
class keyPlusLessThan
{
bool operator()(const keyPlus& lhs, const keyPlus& rhs)
{ return lhs.key < rhs.key; // ? }
};
then the sort call becomes:
sort(keyContainer.begin(),
you can also do it with a pointer to a function if you want, but the function object is probably better. Let me know if you want to see an example with a function pointer.
ASKER
Jason is right. Sort( begin, end ) only works if you have defined operator <.
Ken
Ken
so, is my answer good enough?
or do you need more/different help?
or do you need more/different help?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Jason,
I meant to accept your answer. My excuse is that there was a bit of a delay in getting my rejection accepted (busy network I guess) and then I forgot.
Thanks,
Ken
I meant to accept your answer. My excuse is that there was a bit of a delay in getting my rejection accepted (busy network I guess) and then I forgot.
Thanks,
Ken
No problem, It's sometimes difficult to know what to do around here. Sometimes completely inappropriate answers get accepted, just because they are answers and somebody else has added the answer in a comment.
So it seems better to get an answer in if you think it is appropriate...
So it seems better to get an answer in if you think it is appropriate...
ASKER
Makes sense to me.
Try:
sort(KeyContainer.begin(),
Hope it helps.
Frank