Boost logo

Boost :

From: Gary Powell (Gary.Powell_at_[hidden])
Date: 2001-08-09 14:47:59


[Rob Farrow]
Using STL algorithms...
I am unable to deduce a way of combining or reducing sequence
containers into a single result or filtering arbitrary iterators with
a predicate. So I have created these...

REDUCE: applies a binary operator where the first argument is the
current intermediate result and the second is each value in the
sequence. The result of each function application becomes the new
result.

Example:

vector<int> nums; //contains { 1, 2, 3 }
int result = reduce(nums.begin(), nums.end(), plus<int>(), 0);
//result = 6

[Gary Powell]
  int result = std::accumulate(nums.begin(), nums.end(), 0, plus<int>());

FILTER: a wrapper for an iterator that applies a predicate functor.
The resulting iterator only stops at objects that pass the predicate.

vector<int> nums; //contains { 1, 15, 2, 10, 3 }
int result = reduce(filter(nums.begin(),
                           bind2nd(less_than<int>(),10)),
                    nums.end(), plus<int>(), 0);
//result = 6
[Gary Powell]

   vtl::filter_view<vector<int>, int(*)() > filter(nums,
bind2nd(less_than<int>(),10));

  result = std::accumulate(filter.begin(), filter.end(), 0, plus<int>() );

or use a boost::filter_iterator_generator.

   

GUARD: is the complement of FILTER.
The resulting iterator only stops at objects that FAIL the predicate.

[Gary Powell]
Just reverse the test.

  Just some alternatives.

  Yours,
 -gary-


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk