bind depending on the type

Hi list. I'm trying to find out the way to bind depending on the types in a lambda expression. Let's take the following as an example: #include <iostream> #include <vector> #include <numeric> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> using namespace std; using namespace boost::lambda; int main() { vector< pair<string, int> > 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<string, int>::second, _2)); cout << "total score: " << total << endl; return EXIT_SUCCESS; } 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: #define get_score(p) (bind(&pair<string, int>::second, p)) .. total = accumulate(subjects.begin(), subjects.end(), 0, _1 + get_score(_2)); This seems to be pretty. But the problem is that this code depends on the fact that _1 is int while _2 is pair<string, int>. I don't know whether this is platform independent, and this code still seems to be weird anyway. 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) + get_score(_2)); I've tried my best only to fail. Could anybody write get_score functions for me? Thanks in advance. Sincerely, Minkoo Seo
participants (1)
-
Minkoo Seo