(C++) SumPositives
February 24, 2017 · View on GitHub
(C++) SumPositives
Math code snippet to sum all positive elements in a container.
There are multiple ways to implement SumPositives:
- Using an algorithm (preferred [1][2])
- Using a for-loop
SumPositives using an algorithm
int SumPositives(const std::vector<int>& v) { const std::size_t sz = v.size(); int sum = 0; for (std::size_t i=0; i!=sz; ++i) { if (v[i]>0) sum+=v[i]; } return sum; }
SumPositives using a for-loop
template<typename InputIterator, typename ElementType, typename Predicate> const ElementType accumulate_if( InputIterator first, const InputIterator last, ElementType init, const Predicate predicate) { for (; first != last; ++first) if (predicate(*first)) init += *first; return init; } #include <vector> #include <functional> int SumPositives(const std::vector<int>& v) { return ::accumulate_if(v.begin(),v.end(),0,std::bind2nd(std::greater<int>(),0)); }
References
- Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4. Chapter 18.12.1 : 'Prefer algorithms over loops'
- Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 84: 'Prefer algorithm calls to handwritten loops.'