How to define a template function parameterized by type of function?Accelerated C++ exercise 8-1

L

Lambda

It's from the Accelerated C++ exercise 8-1

The old version is based on pointer to function:

double analysis(const std::vector<Student_info>& students,
double analysis_grade(const Student_info& s))
{
vector<double> grades;

transform(students.begin(), students.end(),
back_inserter(grades), analysis_grade);
return median(grades);
}

void write_analysis(ostream& out, const string& name,
double analysis(const std::vector<Student_info>& students,
double analysis_grade(const Student_info& s)),
double analysis_grade(const Student_info& s),
const vector<Student_info>& did,
const vector<Student_info>& didnt)
{
out << name << ": median(did) = " << analysis(did, analysis_grade) <<
", median(didnt) = " << analysis(didnt, analysis_grade) << endl;
}

// call the functions
write_analysis(cout, "median", analysis, grade_aux, did, didnt);
write_analysis(cout, "average", analysis, average_grade, did, didnt);
write_analysis(cout, "median of homework turned in",
analysis, optimistic_median, did, didnt);

The only difference is the grading function:
grade_aux, average_grade, and optimistic_median.

The question ask me to write a template function,
parameterized by the type of grading function.

How can I do that?

The book doesn't give an example, where can I find more information
about that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top