In article <21e0cb00-9ae3-4f87-805c-13c6679fe9a3
@i36g2000prf.googlegroups.com>,
says...
>
> Let v be:
>
> vector<pair<string, size_t> > v;
>
> sum = 0;
> for (size_t k = 0; k < v.size(); k++)
> {
> sum += v[k].second;
> }
>
> Is it possible to compute sum using std::accumulate (i.e. not using
> for-loop)?
Yes, though it's open to question whether it's really any improvement:
typedef std:

air<std::string, size_t> p;
struct add_second : std::binary_function<size_t, size_t, p> {
size_t operator()(size_t a, p const &b) { return a+b.second; }
};
size_t sum = std::accumulate(v.begin(), v.end(), 0, add_second());
Using Boost.lambda, I believe you should also be able to do something
like this:
size_t sum=std::accumulate(v.begin(), v.end(), 0, var(_1).second);
--
Later,
Jerry.
The universe is a figment of its own imagination.