Accelerated C++ Exercise 8-1: Templates

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Accelerated C++ Exercise 8-1: Templates

Postby unlord213 » Wed Sep 23, 2009 3:04 pm

Hello,
I'm working my way through Accelerated C++, and have run across a problem that I cannot figure out. The text of the question is this:

8-1. Note that the various analysis functions we wrote in §6.2/110 share the same behavior; they differ only in terms of the functions they call to calculate the final grade. Write a template function, parametrized by the type of the grading function, and use that function to evaluate the grading schemes.

The relevant code is this:
Code: Select all
double grade_aux(const Student_info& s)
{
     // compute grade
}

double average_grade(const Student_info& s)
{
     // compute grade
}

// median of the nonzero elements of `s.homework', or `0' if no such elements exist
double optimistic_median(const Student_info& s)
{
     // compute grade
}

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 average_analysis(const vector<Student_info>& students)
{
   vector<double> grades;

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

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

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

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


I have not figured a way to template the grading function being called. The only resources I can find only talk about how to template a particular type. I have come up with this:
Code: Select all
double analysis(const vector<Student_info>& students, double func(const Student_info& s))
{
   vector<double> grades;

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

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


But that doesn't seem to answer the questions, as it is not a template function. Any help on this would be appreciated.
unlord213
 
Posts: 2
Joined: Wed Sep 23, 2009 2:52 pm

Re: Accelerated C++ Exercise 8-1: Templates

Postby jlou » Wed Sep 23, 2009 6:34 pm

Would it be possible to make the functions into types? For example, inside your template function access the templated type object and call analyze(students) or whatever you want to name it. Then create three different classes that each have an analyze function that implements the different type of grading. The function names would all be the same so the templated function you're writing would be the same for all three, but they would be members of different objects for each implementation.
User avatar
jlou
 
Posts: 760
Joined: Tue Sep 21, 2004 6:42 pm

Re: Accelerated C++ Exercise 8-1: Templates

Postby unlord213 » Sun Sep 27, 2009 12:10 am

Technically, the text doesn't get to classes until the next chapter, so that wouldn't be the solution they're looking for. I did manage to get it working, but I don't quite understand it. I have:

Code: Select all
template <class T>
double analysis(const vector<Student_info>& students, T t)
{
   vector<double> grades;

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


but I don't understand why you would call the function parameter a class, "as in class T"
unlord213
 
Posts: 2
Joined: Wed Sep 23, 2009 2:52 pm

Re: Accelerated C++ Exercise 8-1: Templates

Postby jlou » Thu Feb 18, 2010 1:11 am

unlord213 wrote:but I don't understand why you would call the function parameter a class, "as in class T"

That's just the word they chose. You could also use typename instead of class, they are interchangeable there. In this case the class/typename is the function that is called.
User avatar
jlou
 
Posts: 760
Joined: Tue Sep 21, 2004 6:42 pm

Re: Accelerated C++ Exercise 8-1: Templates

Postby ventsyv » Thu Feb 18, 2010 2:17 pm

Yeah class T - think of it as "class Template" or special template class
User avatar
ventsyv
 
Posts: 2810
Joined: Mon Sep 22, 2003 5:25 pm
Location: MD USA

Re: Accelerated C++ Exercise 8-1: Templates

Postby reddemon666 » Tue Aug 31, 2010 10:15 am

Isn't your initial solution the answer to exercise 6-6
Thanks for the template solution, it finally worked after trying to figure it out for quite a while, didn't realise how simple it can be, having done exercise 6-6 initially :)
reddemon666
 
Posts: 1
Joined: Tue Aug 31, 2010 10:09 am

Re: Accelerated C++ Exercise 8-1: Templates

Postby Jiellen_27 » Wed Oct 27, 2010 9:28 pm

Thank you so much for the new details,i really appreciate it....
God bless..
Jiellen_27
 
Posts: 5
Joined: Wed Oct 27, 2010 8:53 pm

Re: Accelerated C++ Exercise 8-1: Templates

Postby Bahnyen » Sun Nov 14, 2010 12:30 am

Your template solution is very useful for me, and for many people too (I think). I'd like to say thank you very much to you.
Bahnyen
 
Posts: 4
Joined: Wed Nov 03, 2010 2:17 am


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 1 guest