BOOST WIKI | STLAlgorithmExtensions | RecentChanges | Preferences | Page List | Links List
I have found for_each_if to be a useful extension -- I often want to perform some (non-mutating) action upon the items in a range that meet some requirements, without changing the contents of the range itself.
- (Note that this implementation is dependent upon the result of the Defect Report submitted by Anthony Williams regarding whether it is legal to dereference an input iterator multiple times. If the resulting clarification indicates that input iterators shall only be dereferenced once, this algorithm will need to be revised in a manner similar to the more elaborate version of CopyIf)
- The committee have responded to the defect report to say "there is no basis for the assumption that an InputIterator? cannot be dereferenced twice", so the following implementation should be correct.
//Performs f() on any element elem in [first, last) where pred(elem) is true
template<typename InputIterator, typename Predicate, typename UnaryFunction>
UnaryFunction for_each_if(InputIterator first, InputIterator last,
Predicate pred, UnaryFunction f)
for(;first != last; ++first)
Maybe collect would have been a much better name than ForEachIf?? If you think of what a garbage collector does -- sort out live blocks from dead blocks by examining them all using some predicate and applying a unary recycling function -- or what a tax collector does -- scan the list of citizens sorting out tax payers from tax exempted using some predicate and then applying an unary dollar extracting function -- well, there you have it. :-)
Disclaimer: This site not officially maintained by Boost Developers