|
Boost Users : |
From: Minkoo Seo (minkoo.seo_at_[hidden])
Date: 2006-08-19 13:54:34
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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net