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?
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?