I am trying to apply the sort() algorithm on a vector of structs. I want the vector to be sorted according to one of the struct fields. I see two ways in which this could be done, and neither seems to work.
Here are the struct and the vector:
Expand|Select|Wrap|Line Numbers
- struct item_calculation
- {
- int item_num;
- float weight;
- float contribution;
- float prediction;
- };
- vector<item_calculation> found_items;
The first way to do the task is defining a comparison function and sending it do sort() as a parameter:
Expand|Select|Wrap|Line Numbers
- bool less_than( item_calculation a, item_calculation b ) {
- return a.prediction < b.prediction;
- };
- .
- .
- .
- sort(found_items.begin(), found_items.end(), less_than);
when using "&CCFEApp::less_than" I get the error: "error C2914: 'std::sort' : cannot deduce template argument as function argument is ambiguous".
The second way to do the task is by overloading the "<" operator:
Expand|Select|Wrap|Line Numbers
- bool operator<(item_calculation a, item_calculation b) {
- return a.prediction < b.prediction;
- };
After changing to:
Expand|Select|Wrap|Line Numbers
- bool operator<(item_calculation b) {
- return prediction < b.prediction;
- };
I would apreciate any help.
Thanks,
Avi.
P.S. I am working under Visual Studio 2005.