std::accumulate has two overloads, one of which takes an operator:
template< class InputIt, class T, class BinaryOperation >
T accumulate( InputIt first, InputIt last, T init,
BinaryOperation op );
, +:
std::accumulate(myMatrix.begin(), myMatrix.end(), Real{0},
[](Real const& sum, std::complex<Real> const& next){
return sum + std::real(next);
});
- , boost::transform_iterator:
auto make_real = [](std::complex<Real> const& c) {
return std::real(c);
};
std::accumulate(
boost::make_transform_iterator(myMatrix.begin(), make_real),
boost::make_transform_iterator(myMatrix.end(), make_real),
Real{0});
range-v3:
accumulate(myMatrix,
Real{0},
std::plus<>{},
[](std::complex<Real> const& c) { return c.real(); }
);
, real , std::real<Real> &std::complex<Real>::real .