The book asks me to do the following:
Note that the function from the previous exercise and the functions from §6.2.2/113 and
§6.2.3/115 do the same task. Merge these three analysis functions into a single function.

the three functions are:

double average_analysis(const vector<Student_info>& students)
{
	vector<double> grades;

	transform(students.begin(), students.end(),
		back_inserter(grades), average_grade);

	return median(grades);
}
double median_analysis(const vector<Student_info>& students)
{
	vector<double> grades;

	transform(students.begin(), students.end(),
		back_inserter(grades), grade_aux); 

	return median(grades);
}
double optimistic_median_analysis(const vector<Student_info>& s)
{
	vector<double> grades;

	transform(s.begin(), s.end(), back_inserter(grades), optimistic_median);

	return median(grades);
}

The only thing that differs if the parameter(which are functions) the function uses in the transform part

Can anyone give me any hints on how to do what he asks ?

is there a way to make that function calling "generic" so I can choose it later ... using only one analysis function ?

Recommended Answers

All 5 Replies

Everything is the same except the last parameter to 'transform'.

So why don't you pass this argument to 1 analysis function?

double analysis( const vector<Student_info>& s, type_of_last_param_to_transform t )
{
 ...
}

I've intentionally left out the type of the last parameter to transform, I assume you can find out what the type of that parameter is.

Everything is the same except the last parameter to 'transform'.

So why don't you pass this argument to 1 analysis function?

double analysis( const vector<Student_info>& s, type_of_last_param_to_transform t )
{
 ...
}

I've intentionally left out the type of the last parameter to transform, I assume you can find out what the type of that parameter is.

That's what I'd tried:

double generic_analysis(const vector<Student_info>& s,double t(const Student_info&))
{
	vector<double> grades;
	transform(s.begin(), s.end(),
		back_inserter(grades), t);
	return 0;
}

and when I called like: "generic_analysis(did, grade_aux);" , for example ... (did being a vector<Student_info>) it only gives me 0 as an answer ... no matter what are the values inside did.

that's why I asked here ... I can't find what I'm doing wrong =S

You should create a pointer to function, and then pass the functions address with a reference.

You should create a pointer to function, and then pass the functions address with a reference.

I haven't got to the part of the book where it deals with pointers ... so I assume there's another way to do so ... any ideas?

Well, I solved it ... thx for your help guys ... the problem was on the second call to these functions ... now it works ... =D thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.