473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Sorting a vector of user defined type

Hi,

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.     };
  8.  
  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.

Thanks,
Avi.

P.S. I am working under Visual Studio 2005.
Sep 12 '06 #1
1 5774
D_C
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. }
  5.  
  6. // list<Person> people;
  7. // Later when I needed to sort I used
  8. people.sort(Person::fname);
Sep 12 '06 #2

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

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
0
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
1
by: jwlkr | last post by:
Hi, I am trying to sort a vector of a user defined type: a class which represents points in cartesian coordinates. The vector of points needs to be sorted according to the value of the...
1
by: tim.lino | last post by:
Dear all, I would like to sort the elements in a vector in a lexicographical order. The elements are defined as: class E { public: int x; int y;
6
by: Tim Frink | last post by:
Hi, I need some help with STL lists. I've a list with pointers to some objects, let's say std::list< ObjectA* myList. The objects of the class ObjectA all have a function "int...
7
by: zl2k | last post by:
hi, all What is the default size of a stl vector? Suppose I have integers needs to store in a vector and I know the max number of integer is, say, 1000. Will it be more efficient that I first...
18
by: =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= | last post by:
Hello. It seems a year is all it takes for one's proficiency in C++ to become too rusty. Professionally, I've been away from the language (and from programming in general), but I still preserve...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.