471,092 Members | 1,034 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

Sorting a vector of user defined type


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
  1.     struct item_calculation 
  2.     {
  3.         int item_num;
  4.         float weight;
  5.         float contribution;
  6.         float prediction;
  7.     };
  9.                vector<item_calculation> found_items; 
I want the vector "found_items" to be sorted by the field "prediction".

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
  1.     bool less_than( item_calculation a, item_calculation b ) {
  2.         return a.prediction < b.prediction;
  3.     };              
  4. .
  5. .
  6. .
  7.     sort(found_items.begin(), found_items.end(), less_than); 
This results with the errors: "error C3867: 'CCFEApp::less_than': function call missing argument list; use '&CCFEApp::less_than' to create a pointer to member."; "error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided"

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
  1.     bool operator<(item_calculation a, item_calculation b) {
  2.         return a.prediction < b.prediction;
  3.     }; 
gives: "error C2804: binary 'operator <' has too many parameters".

After changing to:
Expand|Select|Wrap|Line Numbers
  1.     bool operator<(item_calculation b) {
  2.         return prediction < b.prediction;
  3.     }; 
one gets: "error C2065: 'prediction' : undeclared identifier".

I would apreciate any help.


P.S. I am working under Visual Studio 2005.
Sep 12 '06 #1
1 5502
293 100+
I used a list, but I can't imagine it's that different for vectors.
Edit - This is also STL code.
Expand|Select|Wrap|Line Numbers
  1. static bool fname(Person l, Person r) // Person::fname
  2. {
  3.   return (l.getFname() < r.getFname());
  4. }
  6. // list<Person> people;
  7. // Later when I needed to sort I used
  8. people.sort(Person::fname);
Sep 12 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Alex Vinokur | last post: by
1 post views Thread by tim.lino | last post: by
6 posts views Thread by Tim Frink | last post: by
7 posts views Thread by zl2k | last post: by
18 posts views Thread by =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= | last post: by
5 posts views Thread by jrod11 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.