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

Discussion in 'C++' started by Lambda, Apr 27, 2008.

  1. Lambda

    Lambda Guest

    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?
     
    Lambda, Apr 27, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pete
    Replies:
    14
    Views:
    923
    Roland Pibinger
    Jan 3, 2006
  2. utab

    Accelerated C++ exercise

    utab, Feb 13, 2006, in forum: C++
    Replies:
    2
    Views:
    476
    Diego Martins
    Feb 14, 2006
  3. utab
    Replies:
    3
    Views:
    391
  4. utab
    Replies:
    8
    Views:
    534
    Default User
    Apr 16, 2006
  5. arnuld
    Replies:
    10
    Views:
    753
Loading...

Share This Page