Re: [Boost-users] bind depending on the type
Minkoo Seo wrote:
vector< pair
> subjects; subjects.push_back(make_pair("Math", 90)); subjects.push_back(make_pair("English", 86)); subjects.push_back(make_pair("Physics", 88)); subjects.push_back(make_pair("Biology", 97)); long total; total = accumulate(subjects.begin(), subjects.end(), 0, _1 + bind(&pair
::second, _2));
This is a simple score card application that computes the total score. As you can see, I've used bind in calling accumulate and that seems to be ugly. So, I modified the code as follows:
So, what I want to do is write a function get_score that successfuly compiles and run the following:
total = accumulate(subjects.begin(), subjects.end(), 0, get_score(_1)
Seems fine to me, but it's your code, so your opinion's the important one. +
get_score(_2));
I've tried my best only to fail. Could anybody write get_score functions for me?
You're motivation is unclear. The usual reason to use Boost.Lambda is
that you want to define all of the logic of the algorithm at the call
site, rather than having auxiliary definitions elsewhere. If you're
going to have some part of the algorithm get its own named definition,
then it's simpler just to use a regular function or function object.
Then everything is quite simple:
total = accumulate(subjects.begin(), subjects.end(), 0,
accumulate_scores);
This assumes that accumulate_scores is something like:
int accumulate_scores(int a, pair
participants (1)
-
Todd Greer