Click here to Skip to main content
15,891,529 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Sorting a vector of a user defined type Pin
Zac Howland12-Sep-06 8:11
Zac Howland12-Sep-06 8:11 
AnswerRe: Sorting a vector of a user defined type [modified] Pin
Zac Howland12-Sep-06 4:30
Zac Howland12-Sep-06 4:30 
GeneralRe: Sorting a vector of a user defined type Pin
avimitrani12-Sep-06 5:14
avimitrani12-Sep-06 5:14 
GeneralRe: Sorting a vector of a user defined type Pin
Chris Losinger12-Sep-06 5:16
professionalChris Losinger12-Sep-06 5:16 
GeneralRe: Sorting a vector of a user defined type Pin
avimitrani12-Sep-06 5:42
avimitrani12-Sep-06 5:42 
GeneralRe: Sorting a vector of a user defined type Pin
Zac Howland12-Sep-06 5:40
Zac Howland12-Sep-06 5:40 
GeneralRe: Sorting a vector of a user defined type Pin
avimitrani12-Sep-06 5:49
avimitrani12-Sep-06 5:49 
GeneralRe: Sorting a vector of a user defined type Pin
Zac Howland12-Sep-06 6:17
Zac Howland12-Sep-06 6:17 
Initialization is important, but that won't fix your problem.

The important differences between your code and mine are bolded:

// struct definition
struct item_calculation
{
	int item_num;
	float weight;
	float contribution;
	float prediction;

	item_calculation() : item_num(0), weight(0.0), contribution(0.0), prediction(0.0)
	{}
};

// this is actually a better way of doing it
bool operator< (const item_calculatioin& lhs, const item_calculation& rhs)
{
	return lhs.prediction < rhs.prediction;
}

// sort call
sort(found_items.begin(), found_items.end());	// less<item_calculation> is the default predicate


Having the operator outside the class definition is a better way (and least likely to run into problems when using algorithms) to define it. For classes, you may want to declare them as friend functions as needed.

There is no need to define a new less_than method. By default, sort calls the less<T> functor which calls the overriden operator< for and object of type T (in this case, item_calculation).

Looking back at your original post, it looks like you were trying to declare item_calculation as a private inner class to your App class. Am I correct?

If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

Zac

QuestionConvert from CStringW to CString Pin
zizzzz12-Sep-06 4:05
zizzzz12-Sep-06 4:05 
AnswerRe: Convert from CStringW to CString Pin
Rob Caldecott12-Sep-06 4:31
Rob Caldecott12-Sep-06 4:31 
GeneralRe: Convert from CStringW to CString Pin
zizzzz12-Sep-06 4:53
zizzzz12-Sep-06 4:53 
GeneralRe: Convert from CStringW to CString Pin
Rob Caldecott12-Sep-06 4:58
Rob Caldecott12-Sep-06 4:58 
AnswerRe: Convert from CStringW to CString Pin
Rinu_Raj12-Sep-06 4:35
Rinu_Raj12-Sep-06 4:35 
GeneralRe: Convert from CStringW to CString Pin
zizzzz12-Sep-06 5:21
zizzzz12-Sep-06 5:21 
GeneralRe: Convert from CStringW to CString Pin
David Crow12-Sep-06 7:15
David Crow12-Sep-06 7:15 
AnswerRe: Convert from CStringW to CString Pin
wiemounir12-Sep-06 5:17
wiemounir12-Sep-06 5:17 
GeneralRe: Convert from CStringW to CString Pin
Hamid_RT13-Sep-06 8:11
Hamid_RT13-Sep-06 8:11 
GeneralRe: Convert from CStringW to CString Pin
wiemounir13-Sep-06 23:06
wiemounir13-Sep-06 23:06 
GeneralRe: Convert from CStringW to CString Pin
Hamid_RT13-Sep-06 23:33
Hamid_RT13-Sep-06 23:33 
QuestionBinary was not built with debug information Pin
RugbyLeague12-Sep-06 3:44
RugbyLeague12-Sep-06 3:44 
AnswerRe: Binary was not built with debug information Pin
toxcct12-Sep-06 3:46
toxcct12-Sep-06 3:46 
GeneralRe: Binary was not built with debug information Pin
RugbyLeague12-Sep-06 3:50
RugbyLeague12-Sep-06 3:50 
Questionhelp please!!!!!!!!!!!!!!! Pin
wiemounir12-Sep-06 3:42
wiemounir12-Sep-06 3:42 
AnswerRe: help please!!!!!!!!!!!!!!! Pin
toxcct12-Sep-06 3:48
toxcct12-Sep-06 3:48 
Generalnothing changes Pin
wiemounir12-Sep-06 4:25
wiemounir12-Sep-06 4:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.